在我的 ASP.NET Web 窗体应用程序中,我使用 ASP.NET Identity 2.2 作为成员(member)系统。开发阶段按预期进行。用户经过身份验证,并可以根据其角色访问网站的不同区域。
部署到 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 配置是否有问题?
最佳答案
最终,在尝试了所有可能的解决方案后,我发现问题出在 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 服务器上使用 HTTPS,因此 CookieSecureOption.Always 选项阻止了对后者的身份验证。在这种情况下,默认的 CookieSecureOption.SameAsRequest 选项才是真正正确的选择。
关于asp.net - 为什么用户将 ASP.NET 4.6 应用程序本地部署到 IIS 10 服务器后无法进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37698917/