Configure Unity DI for ASP.NET Identity

后端 未结 4 754
花落未央
花落未央 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条回答
  • 2020-12-01 13:13

    Replace this property in the AccountController

    public ApplicationUserManager UserManager
    {
        get
        { 
            _userManager = new ApplicationUserManager(
                              new UserStore<ApplicationUser>(new ApplicationDbContext()));
            return _userManager ??
                   Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:16

    As this blog post from enterpriseframework.com says:

    First, add the Unity Bootstrapper for ASP.NET MVC Nuget package.

    1. In your Visual Studio "Solution Explorer" > right click on your Web project's "References" node > click "Manage NuGet Packages".

    2. From the left menu, choose Online > All

    3. In the upper right search box > Search Online (Ctrl + E) > type "Unity bootstrapper for ASP.NET MVC".
    4. Select "Unity Bootstrapper for ASP.NET MVC" and choose Install.
    5. Click Close after install completes

    Then Modify your-Web-project/App_Start/UnityConfig.cs file and update the using statements as follows:

        using System;
        using System.Data.Entity;
        using Microsoft.AspNet.Identity;
        using Microsoft.AspNet.Identity.EntityFramework;
        using Microsoft.Practices.Unity;
        using Microsoft.Practices.Unity.Configuration;
        using MicrosoftIdentity.Controllers;
        using MicrosoftIdentity.Models;
    

    Finally, in the same file, update RegisterTypes method as below:

            public static void RegisterTypes(IUnityContainer container)
            {
                // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
                // container.LoadConfiguration();
    
                // TODO: Register your types here
                container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
                container.RegisterType<UserManager<ApplicationUser>>();
                container.RegisterType<DbContext, ApplicationDbContext>();
                container.RegisterType<ApplicationUserManager>();
                container.RegisterType<AccountController>(new InjectionConstructor());
            }
    

    HTH

    0 讨论(0)
  • 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<ApplicationUser> _userManager;
    
    private readonly RoleManager<IdentityRole> _roleManager;
    
    public AccountController(IUserStore<ApplicationUser> userStore, IRoleStore<IdentityRole> roleStore)
    {
      _userManager = new UserManager<ApplicationUser>(userStore);
      _roleManager = new RoleManager<IdentityRole>(roleStore);
    }
    

    Unity Bootstrapper

    var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore));
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(accountInjectionConstructor);
    container.RegisterType<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>(accountInjectionConstructor);
    
    0 讨论(0)
  • 2020-12-01 13:27
    Install-Package Unity.AspNet.WebApi
    

    You need to register Unity under the HttpConfiguration.DependencyResolver property. This allows WebApi to know that it needs to use Unity rather than reflection to instanciate your controllers.

    The easiest way to do this is with the above nuget package.

    0 讨论(0)
提交回复
热议问题