Chr*_*Fin 7 c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
我有一个用于用户身份验证的ASP.NET MVC 5
应用程序ASP.NET Identity 2.1.0
.
过去一切都运行良好,但现在我发现持久的用户会话不再起作用了.我不知道什么样的变化打破了这一点,但它工作时,我实现了身份(转换从应用程序SimpleMembership
),这是我的逻辑我的那一刻:
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)
SignInManager
是我的ApplicationSignInManager
基础SignInManager<ApplicationUser, int>
和model.RememberMe
是true
.
我的设置:
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = ApplicationCookieIdentityValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)
除了持久化用户会话外,一切正常.我检查了我的服务器返回的cookie,并且.AspNet.ApplicationCookie
总是返回"当前会话有效"而不是将来的某个日期.因此,当我关闭并重新打开浏览器时,我需要再次登录...
有没有人知道为什么这不起作用了?
PS:我已经覆盖SignInAsync
了我,ApplicationSignInManager
因为我在那里做了一些自定义逻辑,但我甚至检查了调试器并进行了以下调用:
await base.SignInAsync(user, isPersistent, rememberBrowser);
Run Code Online (Sandbox Code Playgroud)
isPersistent
是的true
,所以它应该创建一个持久cookie.
这是Identity中的一个已知错误,通过查看这个答案,它不是很新.
当每次请求都重新生成cookie时,即使在原始cookie中设置了"IsPersisted"标志,也不会设置.
要解决此问题,您需要实现自己的cookie验证器版本,该验证器将设置标志.
我想我已经为你找到了解决方案,但我没有编译或测试它 - 只是一个方向,你需要去的地方.有关完整代码,请参阅此要点.
这只是SecurityStampValidator
从反编译器中获取的代码.我添加了91-96行.基本上我从前一个cookie中获取"IsPersistent"标志,并在创建时将其添加到新cookie中.这不是在非修改版本中完成的.
然后在你的Auth.Config中你做:
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = MySecurityStampValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
Run Code Online (Sandbox Code Playgroud)
请注意,当新版本出来时,检查这是否已修复,以便您可以删除脏修复.据报道,这个问题是固定的,但是在v2.1发布后不久.