How to check user "logged in"?

I am using authentication using the method below in an ASP.NET application

FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true); 

How to check if a user is logged in or not? And how can I get the registered user username?

+64
c # forms-authentication
May 22 '11 at 6:58
source share
4 answers

I managed to find the right one. He is lower.

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

EDIT

The credit for this edit refers to @Gianpiero Caretti , who suggested this in a comment.

 bool val1 = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated 
+146
May 22 '11 at 7:32
source share

The easiest way to check if they have authenticated Request.User.IsAuthenticated I think (from memory)

+5
May 22 '11 at 7:01
source share

The easiest way:

 if (Request.IsAuthenticated) ... 
+5
May 6 '15 at 18:13
source share
 if (User.Identity.IsAuthenticated) { Page.Title = "Home page for " + User.Identity.Name; } else { Page.Title = "Home page for guest user."; } 
+1
Mar 30 '16 at 12:19
source share



All Articles