How to configure ASP.NET Identity ApplicationUserManager with StructureMap

I use the asp.net identifier in my project and use the structure structure as a DI structure. the problem is that when I use constructor injection, then ApplicationUserManager did not configure all its members, for example, TokenProvider, ...

this is my ApplicationUserManager class:

public class ApplicationUserManager : UserManager<User, long> { public ApplicationUserManager(IUserStore<User, long> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new CustomUserStore(context.Get<InsuranceManagementContext>())); // Configure the application user manager manager.UserValidator = new UserValidator<User, long>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = false }; manager.PasswordValidator = new PasswordValidator { RequireDigit = true, RequiredLength = 8, RequireLowercase = false, RequireNonLetterOrDigit = true, RequireUppercase = false }; var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<User, long>(dataProtectionProvider.Create("TEST")); } return manager; } } 

this is the Startup.Auth class:

 public partial class Startup { // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(InsuranceManagementContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); // Enable the application to use a cookie to store information for the signed in user app.UseCookieAuthentication(new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(2.0), AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active, AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), }); } } 

and its my AccountController:

 public class AccountController : BaseController { private ApplicationUserManager _userManager; public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } } public AccountController(ApplicationUserManager userManager) { UserManager = userManager; } } 

My question is: how can I configure my ApplicationUserManager using a structure? if I install it as the code below, it works, but I don't know if this is a good solution or not:

 ObjectFactory.Initialize(x => { ... x.For<ApplicationUserManager>().Use(() => HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()); ... }); 

please tell me if there is a better solution, and if this is normal, then what is the best term for? HttpContextScope, Singleton, ...?

+8
dependency-injection asp.net-mvc structuremap asp.net-identity asp.net-identity-2
source share
1 answer

Before you create a StructureMap configuration for this, it helps to find out how you will create it manually, that is, if you really “update” everything yourself.

UserManager is dependent on IUserStore, and its implementation EntityFramework (UserStore) is dependent on DbContext. Doing everything manually will look like this:

 var dbContext = new IdentityDbContext("Your ConnectionString Name"); var userStore = new UserStore<IdentityUser>(dbContext); var userManager = new UserManager<IdentityUser>(userStore); 

(Replace IdentityUser with a user user if you use one)

Then you can configure the UserManager as follows:

 userManager.PasswordValidator = new PasswordValidator { RequiredLength = 6 }; 

The hardest part of setting up the userManager is with the UserTokenProvider (which uses api data protection), if you would do it manually, it would look like this:

 var dataProtectionProvider = new DpapiDataProtectionProvider("Application name"); var dataProtector = dataProtectionProvider.Create("Purpose"); userManager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(dataProtector); 

Here is an example StructureMap registry (you can extrapolate from this example and adapt it to your own needs):

  public DefaultRegistry() { Scan( scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.With(new ControllerConvention()); }); For<IUserStore<IdentityUser>>() .Use<UserStore<IdentityUser>>() .Ctor<DbContext>() .Is<IdentityDbContext>(cfg => cfg.SelectConstructor(() => new IdentityDbContext("connection string")).Ctor<string>().Is("IdentitySetupWithStructureMap")); ForConcreteType<UserManager<IdentityUser>>() .Configure .SetProperty(userManager => userManager.PasswordValidator = new PasswordValidator { RequiredLength = 6 }) .SetProperty(userManager => userManager.UserValidator = new UserValidator<IdentityUser>(userManager)); } 

I wrote a blog post about this , it explains the process that leads to this configuration, also a link to an example in the github of the MVC project , where, using this configuration, you can create, write off and delete users.

+5
source share

All Articles