开发者

How to check user is "logged in"?

开发者 https://www.devze.com 2023-03-07 07:56 出处:网络
I am using form authentication with below method in my ASP.NET application FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);

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?


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


The simplest way:

if (Request.IsAuthenticated) ...


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消