用户。身份。IsAuthenticated在第一次点击时总是false?[复制]
-
21-12-2019 - |
题
我有一个asp.net 具有登录屏幕的webforms应用程序。当我点击登录按钮时, User.Identity.IsAuthenticated
在第一次点击时总是错误的。下面是登录的代码:
protected void SignIn(object sender, EventArgs e)
{
var userStore = new UserStore<ApplicationUser>();
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.Find(UserName.Text, Password.Text);
if (user != null && user.PasswordRetryCount < 3)
{
Session["UserName"] = user.UserName;
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties {IsPersistent = false}, userIdentity);
if (User.Identity.IsAuthenticated) //false on first click
Response.Redirect("Default.aspx");
}
}
解决方案 2
而不是这样做:
if (User.Identity.IsAuthenticated)
为了检查用户是否是authenticatd,我确实检查了以下属性 IdentityResult
这似乎工作正常:
if (userIdentity.IsAuthenticated)
其他提示
您已登录用户,但设置IsAuthenticated标志的事件和值在您的下一个请求之前不会运行。您仍处于未验证请求的上下文中。
返回值是什么 authenticationManager.SignIn(..)
?如果它返回bool或任何其他信息,告诉您是否已成功进行身份验证,则可以将其用作您的 if
声明。
的属性用户。身份。IsAuthenticated将根据来自HttpRequest的cookie在application authenticate事件之一中填充。因此,在您的情况下,此属性将具有"false"值。
不隶属于 StackOverflow