转换的用户在30分钟后会被注销。

问题描述 投票:0回答:1

我刚刚将一个.net Framework应用程序转换为.net Core。当从旧的User-table导入数据时,SecurityStamp是小写的guid,例如'0e124deb-8392-4dcc-bce7-38dcc48569a2'。当用户修改密码时,他们会得到一个新的SecurfityStamp,现在它们是大写的,如'0e124deb-8392-4dcc-bce7-38dcc48569a2'。现在它们是大写的,例如'WHBXXXSQEIDVA7KF3T6AJJ3AHWWUSYE'。

使用新的SecurityStamp的用户没有这个问题。我是否应该直接擦除用户表中的SecurityStamp列?是不是在用户下次登录的时候就会创建一个新的SecurityStamp?我很难找到关于身份识别这个层面的文档。

.net core identity .net-core-3.1
1个回答
1
投票

只有当用户更改密码或从外部登录中取消链接时,才会创建一个新的安全印记。Cookie默认以30分钟的时间间隔进行验证。由于您使用的是最新的.net-core版本,因此您可以使用以下代码片段 ConfigureServices()startup.cs 来延长您的验证时间。

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


services.Configure<SecurityStampValidatorOptions>(options =>
{
    // This is the key to control how often validation takes place
    options.ValidationInterval = TimeSpan.FromMinutes(30);
});

: UserManager允许您使用以下方法更新您的安全印记。userManager.UpdateSecurityStampAsync(user).如果你在登录后使用这个,验证很可能会失败。

最后,如果你想用你自己的方式来处理这个行为,你可以写你自己的验证器,并在中间件中进行连接


services.AddAuthentication(options =>
{
  options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
 {
    options.Events.OnValidatePrincipal = LastChangedValidator.ValidateAsync;
 });


public static class LastChangedValidator
{
    public static async Task ValidateAsync(CookieValidatePrincipalContext context)
    {
       // you can use your own logic 

       /* var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
        var userPrincipal = context.Principal;

        // Look for the last changed claim.
        string lastChanged;
        lastChanged = (from c in userPrincipal.Claims
                       where c.Type == "LastUpdated"
                       select c.Value).FirstOrDefault();

        if (string.IsNullOrEmpty(lastChanged) ||
            !userRepository.ValidateLastChanged(userPrincipal, lastChanged))
        {
            context.RejectPrincipal();
            await context.HttpContext.Authentication.SignOutAsync("MyCookieMiddlewareInstance");
        } */
    }
}

或者你可以处理 ValidatePrincipal() 通过使用以下方法覆盖该方法


public class CustomCookieHandler: CookieAuthenticationEvents
{
  public override Task ValidatePrincipal(CookieValidatePrincipalContext context)
  {
    return base.ValidatePrincipal(context);
  }
} 
© www.soinside.com 2019 - 2024. All rights reserved.