2

在我的 ASP.NET Web Forms 应用程序中,我将 ASP.NET Identity 2.2 用于会员系统。开发阶段按预期工作。用户通过身份验证并可以根据他们的角色访问网站的不同区域。

部署到 IIS 10 本地服务器后,身份验证被推翻。登录成功,但用户未进行身份验证。登录页面再次加载空白和新鲜。我知道登录是成功的,通过我在重定向之前创建的文字进行的一些测试。

这是登录方法:

protected void LogIn(object sender, EventArgs e)
    {
        if (IsValid)
        {
            // Validate the user password
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

            List<ApplicationUser> us = manager.Users.ToList();

            foreach (var user in us)
            {
                textSuccess.Text += user.UserName + ": ";
                foreach (var role in user.Roles)
                {
                    textSuccess.Text += role.RoleId + ", ";
                }
            }
            // This doen't count login failures towards account lockout
            // To enable password failures to trigger lockout, change to shouldLockout: true
            var result = signinManager.PasswordSignIn(Email.Text, Password.Text, true, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
                    panelSuccess.Visible = true;
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    break;
                case SignInStatus.LockedOut:
                    Response.Redirect("/Account/Lockout");
                    break;
                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", 
                                                    Request.QueryString["ReturnUrl"],
                                                    RememberMe.Checked),
                                      true);
                    break;
                case SignInStatus.Failure:
                default:
                    FailureText.Text = "Înregistrare eșuată";
                    ErrorMessage.Visible = true;
                    break;
            }
        }
    }

我应该怎么办?集成管道的 OWIN 配置是否有问题?

4

1 回答 1

2

最终,在寻找解决方案的所有可能路径之后,我发现问题出在 Cookie 身份验证的配置上。我会在这里为任何悲惨的研究人员发布原因。

Startup.Auth.cs文件中,我有:

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login.aspx"),
                CookieSecure = CookieSecureOption.Always,
                AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
                Provider = new CookieAuthenticationProvider
                {

                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });

因为我在我的开发服务器上使用 HTTPS,而不是在我部署网站的 IIS 服务器上,所以CookieSecureOption.Always选项阻止了对后者的身份验证。在这种情况下,默认的CookieSecureOption.SameAsRequest选项才是真正正确的选择。

于 2016-06-13T08:36:30.367 回答