views:

60

answers:

1

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 + ASP.Net + IIS 7.0. And I am implementing Forms authentication.

I want to know in Forms authentication, how to check whether a user is already authenticated or not?

thanks in advance, George

+5  A: 

Hi, You can use HttpContext.Current.User.Identity.IsAuthenticated to check whether they're authenticated. For example

if(User.Identity.IsAuthenticated)
{
Response.Write("Logged in already");
}
else
{
Response.Write("Please log in");
}
keyboardP
When User.Identity will be set? In my current code, I just call "FormsAuthentication.SetAuthCookie(someusernumber, false)", will this method set User.Identity?
George2
Yes it will because SetAuthCookie authenticates the user. You may have to reload the page after setting the cookie. A check for authentication after SetAuthCookie will return true.
keyboardP
Thanks, question answered!
George2