Muneer
Muneer

Reputation: 7564

How to check user is "logged in"?

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?

Upvotes: 101

Views: 162954

Answers (4)

Muneer
Muneer

Reputation: 7564

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

Upvotes: 210

isNaN1247
isNaN1247

Reputation: 18099

Easiest way to check if they are authenticated is Request.User.IsAuthenticated I think (from memory)

Upvotes: 7

Yanga
Yanga

Reputation: 3002

if (User.Identity.IsAuthenticated)
{
    Page.Title = "Home page for " + User.Identity.Name;
}
else
{
    Page.Title = "Home page for guest user.";
}

Upvotes: 16

Keith
Keith

Reputation: 21224

The simplest way:

if (Request.IsAuthenticated) ...

Upvotes: 17

Related Questions