c# - User.Identity.IsAuthenticated 在第一次点击时总是错误的?

标签 c# asp.net asp.net-identity

<分区>

我有一个带有登录屏幕的 asp.net webforms 应用程序。当我单击登录按钮时,User.Identity.IsAuthenticated 在第一次单击时始终为 false。下面是登录代码:

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");
        }
    }

最佳答案

您已登录用户,但设置 IsAuthenticated 标志的事件和值不会运行,直到您的下一个请求。您仍在未经身份验证的请求的上下文中。

authenticationManager.SignIn(..) 的返回值是什么?如果它返回一个 bool 或任何其他信息,告诉您您是否成功通过身份验证,您可以将其用作 if 语句的标志。

关于c# - User.Identity.IsAuthenticated 在第一次点击时总是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022595/

相关文章:

c# - ASP.NET Identity 2.1 将 PK 更改为 int 错误

c# - 多个基表不支持动态 sql 生成

c# - 确定反射类型是否可以转换为另一个反射类型

c# - 使用 JavaScript 的服务器端是否确认消息框

c# - ASP .Net Core 在运行时更改路由

asp.net - 如何使用 IdentityServer v3 保护 IdentityManager

javascript - 在打开使用保存的 html 的报告之前在服务器端保存 html 在 Safari 上不起作用

c# - 什么是最快的 winforms 数据网格?

javascript - 如何在 ASP.Net 中单击按钮创建动态表

c# - 如何检测用户何时使用 Microsoft.AspNet.Identity 登录