gpt4 book ai didi

c# - IsPersistent 不起作用 - Cookie 仅对当前 session 有效

转载 作者:太空狗 更新时间:2023-10-29 20:20:23 25 4
gpt4 key购买 nike

我有一个 ASP.NET MVC 5应用程序使用 ASP.NET Identity 2.1.0用于用户身份验证。
过去一切正常,但现在我发现持久化用户 session 不再起作用了。我不知道是什么改变破坏了这个,但是当我实现身份(从 SimpleMembership 转换应用程序)时它起作用了,这是我目前的逻辑:

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

SignInManager是我的 ApplicationSignInManager基于 SignInManager<ApplicationUser, int>model.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);

一切正常,除了持久化用户 session 。我检查了服务器返回的 cookie 和 .AspNet.ApplicationCookie总是返回为“对当前 session 有效”而不是将来的某个日期。因此,当我关闭并重新打开浏览器时,我需要再次登录...

有没有人知道为什么这不再起作用?

P.S.:我已经覆盖了 SignInAsync在我的 ApplicationSignInManager因为我在那里做了一些自定义逻辑,但我什至检查了调试器并检查了以下调用:

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

isPersistenttrue , 因此它应该创建一个持久性 cookie。

最佳答案

This is a known bug in Identity并通过查看 this answer它不是很新。

当每次请求都重新生成 cookie 时,“IsPersisted”标志未设置,甚至在原始 cookie 中设置时也是如此。

要解决这个问题,您需要实现您自己的 cookie 验证器版本,该验证器将按应有的方式设置标志。

我想我已经为您找到了解决方案,但我没有编译或测试它 - 只是您需要去哪里的一般方向。看这个gist for full code .
这只是从反编译器中提取的 SecurityStampValidator 代码。我有 added lines 91-96 .基本上,我从以前的 cookie 中获取“IsPersistent”标志,并在创建新 cookie 时将其添加到新的 cookie 中。在非修改版本中没有这样做。

然后在您的 Auth.Config 中:

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

但请注意,当新版本发布时,请检查此问题是否已修复,以便您可以删除脏修复。这个问题是reported to be fixed ,但在 v2.1 发布后不久。

关于c# - IsPersistent 不起作用 - Cookie 仅对当前 session 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25731661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com