我在我的 ASP.NET 应用程序中使用以下方法进行表单身份验证
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
如何检查用户是否已登录?如何获取已登录用户的用户名?
Easiest way to check if they are authenticated is Request.User.IsAuthenticated I think (from memory)
Request.User.IsAuthenticated
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."; }