Problem with setauthcookie

D

David Colliver

Hi all,

I am having a problem with setting the auth cookie.

I have a username/password box on a page. In my button
event, I am checking a database with the
username/password pair and if I get a row returned, then
I do...

FormsAuthentication.SetAuthCookie(UserNameBox.Text, true);

then do a little bit on my page, such as switching off
panels and switching others on.

Now, on one of the panels that I switch on, I have
linkbuttons which post the page back. I am checking...

if (User.Identity.IsAuthenticated) to do other
operations. This is in my page_load, but IsAuthenticated
is returning false.

I know of no reason. Can anybody help?

My code...

private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize
the page here
//CheckLogin();
if (User.Identity.IsAuthenticated)
{
TabListPanel.Visible =
true;
Trace.Warn("Auth",
DateTime.Now.ToString());
}
else
{
LoginPanel.Visible = true;
}
}

private void LoginButton_Click(object
sender, System.EventArgs e)
{
CheckLogin();
}

private void CheckLogin()
{
try
{
sqlConn.Open();

SqlDataAdapter cmd;

if
(User.Identity.IsAuthenticated)
{
cmd = new
SqlDataAdapter(MyNoneAuthSqlStatement, sqlConn);
}
else
{
cmd = new
SqlDataAdapter(MyAuthSqlStatement, sqlConn);
}

DataSet Login = new
DataSet();
cmd.Fill
(Login, "UserDetails");

if (Login.Tables
["UserDetails"].Rows.Count > 0)
{
// Write the
authentication cookie.

FormsAuthentication.SetAuthCookie
(LoginEmailTextBox.Text, true);
// Remove login
panel, show links (TAB) panel.

LoginPanel.Visible = false;

TabListPanel.Visible = true;
}
}
finally
{
sqlConn.Close();
}
}


This is in my web.config... I have to be careful in here
as this may have issues elsewhere on the site.

<authentication mode="Forms">
<forms loginUrl="MyLogonPage.aspx"
name="adAuthCookie" timeout="60" path="/NQT">
</forms>
</authentication>


Thanks for any assistance.

Best regards,
Dave Colliver.
http://www.SheffieldFOCUS.com
~~
http://www.FOCUSPortals.com - portal franchises available
 
B

Brock Allen

if (User.Identity.IsAuthenticated) to do other operations. This is in
my page_load, but IsAuthenticated is returning false.

That's because for this request the user wasn't authenticated when they came
in.
 
G

Guest

Hi Brock,

Thanks for answering this Q and the one regarding my
usercontrols on the other group.

Granted, the form at initial request won't be
authenticated. This is where I put in a login form. Once
the form has been submitted, then I create an authcookie
in the button submit event.

When I return to the page, I would expect the cookie to
be read which I am assuming would make
User.Identity.IsAuthenticated = true.

Am I missing something?

Thanks.
Dave Colliver.
http://www.ChesterFOCUS.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top