ASP Login Control - Checking login status

10.1k Views Asked by At

I want to use my code behind (C#) to check and see if a user is logged in or not on page load. I assume this is really basic, but I'm still new to .net

This is basically what I want:

if(loggedIn == true){ do something }
5

There are 5 best solutions below

0
Tim B James On BEST ANSWER

If you are using the built in .net Forms Authentication, then you can use the following

if ( HttpContext.Current.User.Identity.IsAuthenticated){
     do something
}

Make sure you have your settings in the web.config set up for using FormsAuthentication

0
mrK On

Check whether User.Identity.Name is an empty string or not.

0
Bala R On
0
srn On
if (User.Identity.IsAuthenticated)
{
    // do something
}

assuming you're using FormsAuthentication.

If however, you're using a Session based login, you can do a nullable check, just like this:

if (HttpContext.Current.Session["somevalue"] != null)
{
    // do something
}
0
Bazzz On

If you are using Membership you can just call GetUser() with no arguments. It results the logged in user if the user is logged in.

http://msdn.microsoft.com/en-us/library/fcxcb339.aspx

Membership.GetUser()