.net 转换后的用户在30分钟后注销

.net 转换后的用户在30分钟后注销,.net,core,identity,.net-core-3.1,.net,Core,Identity,.net Core 3.1,我刚刚使用了一个.NETFramework应用程序,并将其转换为.NETCore。从旧用户表导入数据时,SecurityStamp为小写guid,例如“0e124deb-8392-4dcc-bce7-38dcc48569a2”。当用户更改密码时,他们会得到一个新的securfityamp。现在它们是大写的,例如“whbxxxsqeidva7kf3t6ajj3ahwusye” 具有新SecurityStamp的用户没有问题。我是否应该擦除用户表中的SecurityStamp列?用户下次登录时是否创

我刚刚使用了一个.NETFramework应用程序,并将其转换为.NETCore。从旧用户表导入数据时,SecurityStamp为小写guid,例如“0e124deb-8392-4dcc-bce7-38dcc48569a2”。当用户更改密码时,他们会得到一个新的securfityamp。现在它们是大写的,例如“whbxxxsqeidva7kf3t6ajj3ahwusye”


具有新SecurityStamp的用户没有问题。我是否应该擦除用户表中的SecurityStamp列?用户下次登录时是否创建了新的SecurityStamp?我很难找到这个级别的身份文档。

只有当用户更改密码或与外部登录解除链接时,才会创建新的安全戳。 默认情况下,Cookie的验证时间间隔为30分钟。由于您使用的是最新的.net core版本,因此可以在startup.cs中的ConfigureServices中使用以下代码段来延长验证时间

如果时间设置为0,它将在每个请求中进行验证

services.Configureoptions=> { //这是控制验证频率的关键 options.ValidationInterval=TimeSpan.FromMinutes30; }; 注意:UserManager允许您使用以下方法更新安全戳 userManager.UpdateSecurityStampAsyncuser。如果您在登录后使用此选项,验证很可能会失败

最后,如果您想以自己的方式处理此行为,您可以编写自己的验证器并在中间件中进行连接

services.AddAuthenticationoptions=> { options.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme; }.AddCookieoptions=> { options.Events.OnValidatePrincipal=LastChangedValidator.ValidateAsync; }; 公共静态类LastChangedValidator { 公共静态异步任务ValidateAsyncCookieValidatePrincipalContext上下文 { //你可以用你自己的逻辑 /*var userRepository=context.HttpContext.RequestServices.GetRequiredService; var userPrincipal=context.Principal; //查找上次更改的索赔。 字符串已更改; lastChanged=来自userPrincipal.Claims中的c 其中c.Type==LastUpdated 选择c.Value.FirstOrDefault; 如果string.IsNullOrEmptylastChanged|| !userRepository.ValidateLastChangeduserPrincipal,lastChanged { 语境。主体; wait context.HttpContext.Authentication.SignoutAsynchMyCookieMidleWareInstance; } */ } } 或者,您可以通过使用以下命令重写该方法来处理ValidatePrincipal

公共类CustomCookieHandler:CookieAuthenticationEvents { 公共覆盖任务ValidatePrincipalCookieValidatePrincipalContext上下文 { 返回base.ValidatePrincipalcontext; } }