Configure Unity DI for ASP.NET Identity

后端 未结 4 764
花落未央
花落未央 2020-12-01 13:08

I\'m using Unity successfully for all regular constructor injection such as repositories etc., but I can\'t get it working with the ASP.NET Identity classes. The setup is th

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 13:25

    You also need to resolve the UserManager. The following is an example how you could do it with the UserManager and the RoleManager. In this sample I use the regular Unity 3 package instead of one of the derivates or bootstrappers (had some problems with them in the past).

    AccountController

    private readonly UserManager _userManager;
    
    private readonly RoleManager _roleManager;
    
    public AccountController(IUserStore userStore, IRoleStore roleStore)
    {
      _userManager = new UserManager(userStore);
      _roleManager = new RoleManager(roleStore);
    }
    

    Unity Bootstrapper

    var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore));
    container.RegisterType, UserStore>(accountInjectionConstructor);
    container.RegisterType, RoleStore>(accountInjectionConstructor);
    

提交回复
热议问题