【发布时间】:2017-12-06 16:58:39
【问题描述】:
我创建了两个共享相同身份验证的MVC 应用程序。在应用程序中,我使用可以分配给每个用户的不同用户角色。当我以管理员身份登录时,一切正常,我登录到第一个应用程序并使用相同的 cookie 登录到第二个应用程序,不涉及登录提示。
当我以分配给他们的不同角色的用户身份登录时,登录屏幕会在登录到第一个应用程序后再次弹出并且它不会消失,即使我也在那里登录。
应用程序都在同一个IIS 服务器上。机器密钥在IIS 服务器中配置正确(显然,如果我以分配了管理员角色的用户身份登录,它就可以工作),这是Startup.Auth.cs 中两个应用程序的代码:
第一次申请:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "DefaultCookie",
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
第二次申请:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "DefaultCookie",
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("./Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
OnApplyRedirect = ApplyRedirect
},
});
private static void ApplyRedirect(CookieApplyRedirectContext context)
{
Uri absoluteUri;
if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri))
{
var path = PathString.FromUriComponent(absoluteUri);
Trace.WriteLine(path);
if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath)
context.RedirectUri = "/Account/Login" +
new QueryString(
context.Options.ReturnUrlParameter,
context.Request.Uri.AbsoluteUri);
}
context.Response.Redirect(context.RedirectUri);
}
有谁知道为什么会发生这种情况以及我可以做些什么来解决它?
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 iis