Search code examples
dependency-injectionunity-containerasp.net-identityasp.net-web-api2

How to inject ApplicationUserManager with unity


I have ApplicationUserManager defined like this:

public class ApplicationUserManager : UserManager<ApplicationUser, int>
    { 
        public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
        : base(store)
        {
        }

       public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
        {
            var result = base.CreateAsync(user, password);
            //... 
            Repository.DoSomething(...); //Repository is null here
            //..
        }          

       [Dependency]
       public IRepository Repository { get; set; }
    }

For some reason my repository in not getting injected. Repository is always null

I also have this line in my unity configuration

.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager())

How to make it injected?

UPDATE N:

This is code from my controller; How I am getting UserManager:

public ApplicationUserManager UserManager
{
    get
    {
        return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}

public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
         //calling my implemintation
         IdentityResult result = await UserManager.CreateAsync(...);
    }

My current unity config

.RegisterType<IUserStore<ApplicationUser, int>, CustomUserStore>(new HierarchicalLifetimeManager())
.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication))
.RegisterType<UserManager<ApplicationUser, int>, ApplicationUserManager>()
.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<AccountController>(new InjectionConstructor())
.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<ApplicationUserManager>()

UPDATE N+1:

As I find out Unity.Mvc package is not injecting WebApi controllers. I used this method in order to inject WebApi controllers. I was trying to remove Unity.Mvc package but got Identity throwing errors. From my understanding because Unity was not able to instantiate some Identity types however they were being configured and worked in the case of setting Mvc container. So I brought back the Unity.Mvc package to inject my Identity types. Otherwise, as I explained above, it is throwing different kind on null refs when Unity resolving Identity types.

So I have two containers now in my project Unity.Mvc injection DependencyResolver.SetResolver(new UnityDependencyResolver(container)); that needed to resolve Identity types and custom WebApi injection config.DependencyResolver = new UnityResolver(container); container that needed to inject WebApi contorllers both using same UnityConfig.

UPDATE 3:

I've changed this:

app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());

to this

app.CreatePerOwinContext(() => UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>());

in Startup.ConfigureAuth() and now I my WebApi container is fully working and constructing all required Identity types so I can disable Mvc container or even completely can remove Unity.Mvc package.

Thanks to @Sam Farajpour Ghamari for clerifying lots of things.


Solution

  • Since you are using UserManager with OWIN. You need to integrate OWIN container with unity. Where as I don't see your other part of codes specially OWIN startup method. I show a simple example to demonstrate how you can do this by own.

    First if you are using Entity Framework you must register your context to unity. Second register other type which Identity need them like this:

    container.RegisterType<DbContext, MyDbContext>(new PerRequestLifetimeManager());
    container.RegisterType<IUserStore<ApplicationUser>,
        UserStore<ApplicationUser>>(new PerRequestLifetimeManager());
    container.RegisterType<ApplicationUserManager>(new PerRequestLifetimeManager());
    container.RegisterType<IAuthenticationManager>(
        new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication))
    container.RegisterType<ApplicationSignInManager>(new PerRequestLifetimeManager());
    

    Then change constructor of ApplicationUserManager like this:

    public ApplicationUserManager(IUserStore<ApplicationUser> store,
        IRepository repo)
            : base(store)
    {
        this.Repository=repo;
        // put your other configuration here instead of putting in 
        // static ApplicationUserManagerCreate() method.
        this.UserValidator = new UserValidator<ApplicationUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
            // Configure validation logic for passwords
        this.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };
    
            // Configure user lockout defaults
        this.UserLockoutEnabledByDefault = true;
        this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        this.MaxFailedAccessAttemptsBeforeLockout = 5;
    
        // and your other configurations
    }
    

    Now in your Startup.ConfigureAuth() method change following lines:

    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(EFDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        // others
    }
    

    to:

    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(()=> DependencyResolver.Current.GetService<ApplicationUserManager>());
        app.CreatePerOwinContext(()=> DependencyResolver.Current.GetService<ApplicationSignInManager>());
        // other configs
    }
    

    Also we don't need ApplicationUserManager Create() and ApplicationSignInManager.Create() methods anymore and we could easily remove them, since Unity takes responsibility of create our classes now.

    Now we fully integrate Identity with unity. For further and complex information I deeply encourage you to read this awesome blog post.