IsPersistent不工作-Cookie仅对当前会话有效



我有一个使用ASP.NET Identity 2.1.0进行用户身份验证的ASP.NET MVC 5应用程序
过去一切都很好,但现在我发现持久化用户会话已经不起作用了。我不知道是什么变化打破了这一点,但当我实现Identity(从SimpleMembership转换应用程序)时,它起了作用,这就是我目前的逻辑:

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, 
                                     model.RememberMe, shouldLockout: true);

SignInManager是我在SignInManager<ApplicationUser, int>基础上的ApplicationSignInManagermodel.RememberMetrue

我的设置:

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

除了持久化用户会话之外,一切都很好。我检查了服务器返回的cookie,.AspNet.ApplicationCookie始终返回为"对当前会话有效",而不是将来的某个日期。所以当我关闭并重新打开浏览器时,我需要再次登录。。。

有人知道为什么这不起作用吗

附言:我在ApplicationSignInManager中重写了SignInAsync,因为我在那里做了一些自定义逻辑,但我甚至检查了调试器和以下调用:

await base.SignInAsync(user, isPersistent, rememberBrowser);

isPersistenttrue,所以它应该创建一个持久cookie。

这是Identity中的一个已知错误,通过查看这个答案,它并不是很新。

当对每个请求重新生成cookie时,即使在原始cookie中设置了"IsPersisted"标志,也不会设置该标志。

为了解决这个问题,您需要实现自己版本的cookie验证器,该验证器将按原样设置标志。

我想我已经为你找到了解决方案,但我还没有编译或测试它——只是你需要去哪里的大致方向。有关完整代码,请参阅此要点
这只是从反编译器中获取的SecurityStampValidator代码。我增加了第91-96行。基本上,我从以前的cookie中获取"IsPersistent"标志,并在创建新cookie时将其添加到新cookie中。在未修改的版本中没有这样做。

然后在您的Auth.Config中执行:

Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = MySecurityStampValidator.OnValidateIdentity(
                    validateInterval: TimeSpan.FromMinutes(0),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }

不过要注意,当新版本发布时,请检查是否已修复,这样您就可以删除脏修复。据报道,这个问题已经修复,但在v2.1发布后不久。

AspNet.Identity.CoreAspNet.Identity.Owin都更新到2.2.1应该可以解决此问题。

为了在Mvc缩进中关闭浏览器上的用户日志。下面的代码是我在Startup.Auth.cs类中使用的代码。

app.UseCookieAuthentication(new CookieAuthenticationOptions
                    { 
                        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                        SlidingExpiration = true,
                        CookieHttpOnly = false,
                        LoginPath = new PathString("/Account/Login"),
                        Provider = new CookieAuthenticationProvider
                        {  
                            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                            validateInterval: TimeSpan.FromMinutes(30),
                                regenerateIdentity: (manager, user) => 
                                user.GenerateUserIdentityAsync(manager)),
                            OnResponseSignIn = context => {
                                context.Properties.IsPersistent = true; };
                            } 
        }

最新更新