【问题标题】:How Does One Turn Off the SecurityStamp Feature in ASP.NET Core Identity如何关闭 ASP.NET Core Identity 中的 SecurityStamp 功能
【发布时间】:2021-06-14 08:24:22
【问题描述】:

我从使用 Identity Server 中学到的一件事是,开箱即用,ASP.NET Core Identity 不能很好地与 Identity Server 配合使用。参考this question

ASP.NET 身份 包含一种在特定用户的数据已更改时弹出用户的方法。这受 aspnetuser 表的 SecurityStamp 列的约束。当标记更改时,下次验证 cookie 时,验证失败。

这是默认开启的,这也是它不能很好地与 Identity Server 配合使用的原因。
您最终会导致用户被无规律地踢出。当你期望它踢出用户时,它不会。

无论如何,如何禁用ASP.NET Identity的这个功能???

UserManager 上有一个名为SupportsUserSecurityStamp 的列。 但是,它只是一个“getter”,我找不到任何配置设置来切换它以返回 false

在我看来,唯一的方法是从 UserManager 类派生并让该属性只返回false

这感觉就像一个黑客。尤其是当您在 Github 上查看该属性的代码时:

public virtual bool SupportsUserSecurityStamp
{
    get
    {
        ThrowIfDisposed();
        return Store is IUserSecurityStampStore<TUser>;
    }
}

是否有“正确的方法”来关闭 ASP.NET Identity 的 SecurityStamp 功能?

旁注:目前,我们有一个创可贴的解决方案,通过这种配置使巨大的验证时间变得很长-

services.Configure<SecurityStampValidatorOptions>(
    o => o.ValidationInterval = TimeSpan.FromHours(SecurityTimestampDuration)
);

所有这一切都是在印章验证之间建立一个巨大的窗口。但是完全关闭该功能更为可取。

干杯

【问题讨论】:

    标签: asp.net-identity asp.net-core-identity


    【解决方案1】:

    我关闭了SecurityStamp 功能,如问题中所述。 因此,我创建了一个继承自 UserManager 类的类。 应该看起来像这样:

    public class IcUserManager : UserManager<IdpUser>
    {
        public IcUserManager(IUserStore<IdpUser> store, 
            IOptions<IdentityOptions> optionsAccessor, 
            IPasswordHasher<IdpUser> passwordHasher, 
            IEnumerable<IUserValidator<IdpUser>> userValidators, 
            IEnumerable<IPasswordValidator<IdpUser>> passwordValidators, 
            ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, 
            IServiceProvider services, 
            ILogger<UserManager<IdpUser>> logger) 
            : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
        }
    
        public override bool SupportsUserSecurityStamp => false;
    }
    

    通过将SupportsUserSecurityStamp 设置为false,它会立即退出框架中的验证检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 2021-12-20
      • 2018-07-04
      • 2010-09-08
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多