问题描述
我正在使用 ASP.NET 框架 CookieAuthenticationProvider 生成具有 AspNet.Identity.Core 版本 2.2.2 的身份。
当我从前端查看它时,cookie 似乎是正确生成的(CookieName、CookieDomain 都符合预期)。
但是,我希望 cookie 在每 X 秒后刷新一次。在 Microsoft 文档中,它声明我可以为此使用 CookieAuthenticationProvider 对象上的 OnValidateIdentity 属性,但是regenerationIdentityCallback 似乎永远不会被触发。
需要提及的一件重要事情是我们在 UserManager
当前代码如下所示:
app.UseCookieAuthentication(new CookieAuthenticationoptions
{
AuthenticationType = "Identity.Application",CookieName = $".AspNet.SharedCookie-{environment}",CookieDomain = ".example.com",LoginPath = new PathString("/"),Provider = new CookieAuthenticationProvider
{
OnValidateIdentity =
SecurityStampValidator
.OnValidateIdentity<UserManager<User,int>,User,int>(
validateInterval: TimeSpan.FromSeconds(30),regenerateIdentityCallback: async (manager,user) =>
{
var identity = await manager.CreateIdentityAsync(user,"Identity.Application");
return identity;
},getUserIdCallback: (user) => Int32.Parse(user.GetUserId()))
},TicketDataFormat = new AspNetTicketDataFormat(
new DataProtectorShim(
DataProtectionProvider.Create(keyRingFolderInfo,(builder) => { builder.SetApplicationName($"{environment}-{applicationName}"); })
.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware","Identity.Application","v2"))),CookieManager = new ChunkingCookieManager()
});
为什么 ValidateInterval 不会每 30 秒重新生成一次身份?我还应该如何让它按照我想要的方式工作?
解决方法
既然你有一个 int 键,你就实现了一个自定义的 UserManager、UserStore、(...)
当你实现自己的逻辑时,你也必须实现这个接口:
[IUserSecurityStampStore<TUser,in TKey>]
在您的自定义 UseStore 类 (more infos about this interface) 中。
在这里您可以看到 SecurityStampValidator 的默认实现。
// Only validate if enough time has elapsed
var validate = (issuedUtc == null);
if (issuedUtc != null)
{
var timeElapsed = currentUtc.Subtract(issuedUtc.Value);
validate = timeElapsed > validateInterval;
}
if (validate)
{ ..... await regenerateIdentityCallback.Invoke(manager,user).WithCurrentCulture()
如您所见,此类决定调用 regenerateIdentityCallback 方法。调试这个方法,你就会明白为什么会调用 regenerateIdentityCallback。