如何在IdentityServer DI框架下注册ApplicationUserManager?

 2023-02-15    326  

问题描述

我正在使用IdentityServer3进行身份验证.所有用户都存储在SQL DB中,因此我还使用Microsoft.AspNet.Identity框架进行实际身份验证,并且出于相同的目的,我创建了自己的ApplicationUserManager类.

ASPNET身份已将IOC功能集成到OWIN中间件中.它注册ApplicationUserManager喜欢:

如何在IdentityServer DI框架下注册ApplicationUserManager?

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

它返回ApplicationUserManager>

的新实例的功能委托

public static ApplicationUserManager 
Create(IdentityFactoryOptions<ApplicationUserManager> options,
        IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

但是,IdentityServer使用其自己的DI框架,( i认为)我们不能使用static Create()使用sidentityServer来使用static Create() rgister ApplicationUserManager,Create()方法也可以接受IdentityFactoryOptions IOwinContext作为参数.

我遵循此 我更改了ApplicationUserManager使用构造器注入的实现

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{   
    public ApplicationUserManager(ApplicationUserStore store, IdentityFactoryOptions<ApplicationUserManager> options)
        : base(store)
    {          
        // Configure validation logic for usernames
        UserValidator = new UserValidator<ApplicationUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        UserLockoutEnabledByDefault = true;
        DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        MaxFailedAccessAttemptsBeforeLockout = 5;

        EmailService = new EmailService();
        SmsService = new SmsService();

        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }        
}

,然后在以下

下用身份服务器自己的DI框架注册所有服务

factory.UserService = new Registration<IUserService, UserService>();
factory.Register(new Registration<ApplicationUserManager>());
factory.Register(new Registration<ApplicationUserStore>());
factory.Register(new Registration<IdentityFactoryOptions<ApplicationUserManager>>(resolver => new IdentityFactoryOptions<ApplicationUserManager>
            {
                DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET Identity")
            }));
factory.Register(new Registration<ApplicationDbContext>(resolver => new ApplicationDbContext(ApplicationConfig.ConnectionString)));

问题

  1. 这是使用注册的正确方法ApplicationUserManager
    Identiserver的DI框架?
  2. 我在注册过程中创建DataProtectionProvider,在构造函数内部创建UserTokenProvider.身份服务器如何使用这两个提供商?
  3. 请注意,我没有在任何地方注册IOwinContext
    ApplicationUserManager的构造函数不再需要它,将
    这导致Owin管道的任何问题?
  4. ApplicationUserManager>>
  5. 的理想注册模式是什么

推荐答案

这两篇文章有助于解决我的问题

但是,我的ApplicationUserManager在单独的类库中,startup.cs在Web项目中.类库没有引用Web项目.因此,我重构ApplicationUserManager构造函数

public ApplicationUserManager(ApplicationUserStore store, IDataProtectionProvider dataProtectionProvider)
        : base(store)
    {
       // other stuff


        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("UserToken"));
        }
    }

以及带有DI框架的注册IDataProtectionProvider.我不使用Unity作为IOC.我正在使用IdentityServer自己的DI框架.所以我注册IDataProtectionProvider为

factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider));

以上所述是小编给大家介绍的如何在IdentityServer DI框架下注册ApplicationUserManager?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33763.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。