简体   繁体   English

如何检查用户是“登录”?

[英]How to check user is “logged in”?

I am using form authentication with below method in my ASP.NET application 我在ASP.NET应用程序中使用下面的方法进行表单身份验证

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. 这个编辑的功劳归功于@Gianpiero Caretti ,他在评论中提出了这个建议。

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

最简单的方法:

if (Request.IsAuthenticated) ...

检查它们是否经过身份验证的最简单方法是Request.User.IsAuthenticated我认为(来自内存)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM