8

Ich benutze asp.net Identität in meinem Projekt und mit structurmap als DI-Framework. das Problem ist, wenn ich Konstruktor Injektion verwenden dann ApplicationUserManager nicht all seine Mitglieder, zB TokenProvider konfiguriert, ...Wie konfiguriert man ASP.NET Identity ApplicationUserManager mit StructureMap

das ist mein ApplicationUserManager Klasse:

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; 
    } 
} 

dies Startup.Auth Klasse:

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"), 
     }); 
    } 
} 

und seine meinem Account:

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; 
    } 
} 

mein qu Wie kann ich meinen ApplicationUserManager mit Strukturmap konfigurieren? , wenn ich es als Code unten gesetzt funktioniert es, aber ich weiß nicht, es ist eine gute Lösung ist oder nicht:

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

bitte mich andeuten, wenn es eine bessere Lösung ist, und ob es in Ordnung ist, was ist dann die beste lebensdauer dafür? HttpContextScope, Singleton, ...?

+2

Ich habe gerade über Identity und DI-Container gebloggt: http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/ das wird Ihnen wahrscheinlich helfen. – trailmax

Antwort

5

Bevor Sie die StructureMap-Konfiguration für diese erstellen, hilft es zu wissen, wie Sie es manuell erstellen würden, d. H. Wenn Sie tatsächlich alles selbst neu erstellen.

UserManager hat eine Abhängigkeit von IUserStore und seine EntityFramework-Implementierung (UserStore) hat eine Abhängigkeit von DbContext. alles tun, wie dies manuell aussehen:

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

(Ersetzen IdentityUser mit Ihrem eigenen Benutzer, wenn Sie eines verwenden)

Sie dann UserManager wie folgt konfigurieren:

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

Die Der komplizierteste Teil zum Konfigurieren von userManager bezieht sich auf den UserTokenProvider (der die Datenschutz-API verwendet), wenn Sie es manuell tun würden, würde es look like this:

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

Hier ein Beispiel für eine StructureMap Registrierung ist (Sie können von diesem Beispiel extrapolieren und es an die eigenen Bedürfnisse anpassen):

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));     
    } 

ich einen Blog post about this, es erklärt den Prozess schrieb, die zu diesem führen Konfiguration, gibt es auch eine link to an example on github of an MVC project wo, mit dieser Konfiguration können Sie Benutzer erstellen, auflisten und löschen.