AspNetUsermanager 和 UserManager 之间的区别

Kon*_*ten 6 c# identity user-management asp.net-core

注意:我收到警告说有很多类似的问题。然而,这些主要涉及其他概念,例如用户管理器和用户存储之间的差异(以及一堆不相关事物之间的差异,所以我猜测 Stacky 的权重差异相同)。然而,谷歌搜索差异 AspNetUserManager UserManager产生了很多关于如何设置安全性的指南,但没有讨论实际的差异。

所以,我去了古老的 MSDN 并查找了这两个类。显然,AspNetUsermanager是这样描述的。

提供用于管理持久性存储中的用户的 API。

UserManager是这样描述的。

提供用于管理持久性存储中的用户的 API。

鉴于两者都是用于管理用户,人们会期望这两者之间的功能存在一定的重叠。然而,我觉得读者可能应该在真实的表述中看到更多的变化。

我的问题是 - 它们如何关联(即在什么情况下前者优于后者)?

Jul*_*ian 9

如果我检查代码,唯一的区别是AspNetUserManager<TUser>从 httpcontext 获取(默认)cancelationtoken(根据请求中止)。我想这会在取消请求时提供更好的体验。所有其他方法和属性都继承自UserManager<TUser>

/// <summary>
    /// Provides the APIs for managing user in a persistence store.
    /// </summary>
    /// <typeparam name="TUser">The type encapsulating a user.</typeparam>
    public class AspNetUserManager<TUser> : UserManager<TUser>, IDisposable where TUser : class
    {
        private readonly CancellationToken _cancel;

        /// <summary>
        /// Constructs a new instance of <see cref="AspNetUserManager{TUser}"/>.
        /// </summary>
        /// <param name="store">The persistence store the manager will operate over.</param>
        /// <param name="optionsAccessor">The accessor used to access the <see cref="IdentityOptions"/>.</param>
        /// <param name="passwordHasher">The password hashing implementation to use when saving passwords.</param>
        /// <param name="userValidators">A collection of <see cref="IUserValidator{TUser}"/> to validate users against.</param>
        /// <param name="passwordValidators">A collection of <see cref="IPasswordValidator{TUser}"/> to validate passwords against.</param>
        /// <param name="keyNormalizer">The <see cref="ILookupNormalizer"/> to use when generating index keys for users.</param>
        /// <param name="errors">The <see cref="IdentityErrorDescriber"/> used to provider error messages.</param>
        /// <param name="services">The <see cref="IServiceProvider"/> used to resolve services.</param>
        /// <param name="logger">The logger used to log messages, warnings and errors.</param>
        public AspNetUserManager(IUserStore<TUser> store,
            IOptions<IdentityOptions> optionsAccessor,
            IPasswordHasher<TUser> passwordHasher,
            IEnumerable<IUserValidator<TUser>> userValidators,
            IEnumerable<IPasswordValidator<TUser>> passwordValidators,
            ILookupNormalizer keyNormalizer,
            IdentityErrorDescriber errors,
            IServiceProvider services,
            ILogger<UserManager<TUser>> logger)
            : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
            _cancel = services?.GetService<IHttpContextAccessor>()?.HttpContext?.RequestAborted ?? CancellationToken.None;
        }

        /// <summary>
        /// The cancellation token associated with the current HttpContext.RequestAborted or CancellationToken.None if unavailable.
        /// </summary>
        protected override CancellationToken CancellationToken => _cancel;
   }
Run Code Online (Sandbox Code Playgroud)

来自https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Core/src/AspNetUserManager.c

非常相关,有一个建议去掉这个类:

考虑向管理器 API 添加可选的取消令牌

如果我们在 3.0 中这样做,我们可能还可以摆脱自动 HttpRequest.Aborted 连接,这将使我们完全摆脱派生的 AspNetUserManager。

请参阅https://github.com/dotnet/aspnetcore/issues/5763