How to check user is "logged in"?

BlueBird picture BlueBird · May 22, 2011 · Viewed 131.1k times · Source

I am using form authentication with below method in my ASP.NET application

FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);

How do I check whether user is logged in or not? And how can I get the user name of a logged in user?

Answer

BlueBird picture BlueBird · May 22, 2011

I managed to find the correct one. It is below.

bool val1 = System.Web.HttpContext.Current.User.Identity.IsAuthenticated

EDIT

The credit of this edit goes to @Gianpiero Caretti who suggested this in comment.

bool val1 = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated