Setup Simple Injector with ASP.NET Identity 2.2.1



所以我一直在尝试设置ASP。NET Identity 2.2.1和我不断遇到障碍。作为一个应该由框架自动生成的东西,配置起来确实很复杂。我想我已经差不多了,但我仍然在用Simple Injector设置IoC时遇到问题。

我一直在阅读关于这些问题的其他StackOverflow问题和博客。他们中的大多数人似乎已经过时了,因为他们与ASP打交道。NET Identity 1.0。从2.0到2.2.1甚至有足够多的变化,我遇到了一些过时解决方案的问题。或者解决方案处理其他IoC框架,而我无法将它们转换为Simple Injector。我使用过其他IoC框架,但我是Simple Injector的新手。

更新-Ric。Net在评论中的第一个链接非常有用。我已经重新调整了我的工作以适应他的建议,我想我已经差不多了。但我仍然有一个问题。我已经完成了用下面的代码替换以前的代码。

1)我更新了ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(string connectionstring)
        : base(connectionstring, throwIfV1Schema: false)
    {
    }
}

2)我从ApplicationUserManager中删除了Create()方法,并将其移动到一个初始化所有Simple Injector代码的类中:

public static class SimpleInjectorInitializer
{
    public static Container Initialize(IAppBuilder app)
    {
        var container = GetInitializeContainer(app);
        container.Verify();
        DependencyResolver.SetResolver(
            new SimpleInjectorDependencyResolver(container));
        return container;
    }
    public static Container GetInitializeContainer(
              IAppBuilder app)
    {
        var container = new Container();
        container.RegisterSingle<IAppBuilder>(app);
        container.RegisterPerWebRequest<
               ApplicationUserManager>();
        container.RegisterPerWebRequest<ApplicationDbContext>(() 
          => new ApplicationDbContext(
           "Your constring goes here"));
        container.RegisterPerWebRequest<IUserStore<
          ApplicationUser>>(() => 
            new UserStore<ApplicationUser>(
              container.GetInstance<ApplicationDbContext>()));
        container.RegisterInitializer<ApplicationUserManager>(
            manager => InitializeUserManager(manager, app));
        container.RegisterMvcControllers(
                Assembly.GetExecutingAssembly());
        return container;
    }
    private static void InitializeUserManager(
        ApplicationUserManager manager, IAppBuilder app)
    {
        manager.UserValidator = 
         new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        //Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator()
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };
        var dataProtectionProvider = 
             app.GetDataProtectionProvider();
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = 
             new DataProtectorTokenProvider<ApplicationUser>(
              dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }
}

3)我在启动中更改了配置方法。cs

public void Configuration(IAppBuilder app)
{
    var container = SimpleInjectorInitializer.Initialize(app);
    ConfigureAuth(app, container);
}

4)我在配置中更新了ConfigureAuth。Auth.cs

public void ConfigureAuth(IAppBuilder app, Container container)
{
    // Configure the db context and user manager to use a single instance per request
    //app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext(container.GetInstance<ApplicationUserManager>);
    // Rest of code here ...

5)然后我从AccountsController中删除了默认构造函数和UserManager属性。

我现在遇到的问题是,自从ASP。NET Identity 2.0,他们已经将参数ISecureDataFormat<AuthenticationTicket> accessTokenFormat添加到AccountsController构造函数中。

public AccountController(ApplicationUserManager userManager, 
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)

那么现在,如何在Simple Injector中为ISecureDataFormat<AuthenticationTicket>添加DI?

所以在帮助Ric之间。Net(我在上面发布了这个问题的一部分)和Lucas Teles在另一个问题中的回答,我终于得到了答案。Lucas建议将这些行添加到我的Simple Injector设置中:

container.Register<ISecureDataFormat<AuthenticationTicket>,
    SecureDataFormat<AuthenticationTicket>>(Lifestyle.Scoped);
container.Register<ITextEncoder, Base64UrlTextEncoder>(Lifestyle.Scoped);
container.Register<IDataSerializer<AuthenticationTicket>, TicketSerializer>(
    Lifestyle.Scoped);
container.Register<IDataProtector>(
    () => new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider()
        .Create("ASP.NET Identity"),
    Lifestyle.Scoped);

所以现在我的容器构建方法是这样的:

public static Container GetInitializeContainer(IAppBuilder app)
{
    var container = new Container();
    // IoC for ASP.NET Identity
    container.RegisterSingleton<IAppBuilder>(app);
    container.Register<ApplicationUserManager>(Lifestyle.Scoped);
    container.Register<ApplicationDbContext>(
        () => new ApplicationDbContext("Your constring goes here"),
        Lifestyle.Scoped);
    container.Register<IUserStore<ApplicationUser>>(
        () => new UserStore<ApplicationUser>(
            container.GetInstance<ApplicationDbContext>()),
        Lifestyle.Scoped);
    container.RegisterInitializer<ApplicationUserManager>(
        manager => InitializeUserManager(manager, app));
    // Setup for ISecureDataFormat
    container.Register<ISecureDataFormat<AuthenticationTicket>, 
        SecureDataFormat<AuthenticationTicket>>(Lifestyle.Scoped);
    container.Register<ITextEncoder, Base64UrlTextEncoder>(Lifestyle.Scoped);
    container.Register<IDataSerializer<AuthenticationTicket>, 
        TicketSerializer>(Lifestyle.Scoped);
    container.Register<IDataProtector>(
        () => new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider()
            .Create("ASP.NET Identity"),
        Lifestyle.Scoped);
    // Register all controllers
    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
    return container;
}

相关内容

  • 没有找到相关文章

最新更新