SimpleInjector和MVC Identity 2FA

时间:2018-09-20 13:01:19

标签: asp.net-mvc-4 asp.net-identity simple-injector

实现Simpleinjector之后,MVC身份TwoFactorAuthentication停止工作。我怀疑这是因为Simpleinjector不使用WiasApplicationUserManager的注册实例,而是创建了一个新实例,因此初始化丢失了。

Startup.Auth.cs中,我有:

app.CreatePerOwinContext<WiasApplicationUserManager>(WiasApplicationUserManager.Create);
app.CreatePerOwinContext<WiasSignInManager>(WiasSignInManager.Create);

SimpleInjectorInitializer

private static void InitializeContainer(Container container)
    {
        container.Register<IUserStore<WiasApplicationUser, int>>(
            () => new WiasUserStore(), Lifestyle.Scoped);

        container.Register(
            () => new UserManager<WiasApplicationUser, int>(
                new WiasUserStore()), Lifestyle.Scoped);

        container.Register(() =>
                container.IsVerifying
                    ? new OwinContext().Authentication
                    : HttpContext.Current.GetOwinContext().Authentication,
            Lifestyle.Scoped);


        container.Register<IViewModel<UserViewModel>, UserViewModel>();
        container.Register<IViewModel<UsersViewModel>, UsersViewModel>();

        container.Register(typeof(ICommandHandler<>), typeof(ICommandHandler<>).Assembly);
        container.Register(typeof(IQueryHandler<,>), typeof(IQueryHandler<>).Assembly);
    }

TwoFactorProviders已在WiasApplicationUserManager.Create()中注册

manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<WiasApplicationUser, int>
        {
            MessageFormat = "Your security code is {0}"
        });
        manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<WiasApplicationUser, int>
        {
            Subject = "Security Code",
            BodyFormat = "Your security code is {0}"
        });

我该如何配置它,以便当Simpleinjector将其注入到WiasApplicationUserManager.Create()中时,在AccountController中完成的配置不会丢失?

我尝试过:

container.Register<WiasSignInManager>(() =>
        {
            var cbase = new HttpContextWrapper(HttpContext.Current);
            return cbase.GetOwinContext().Get<WiasSignInManager>();
        });

但是cbase.GetOwinContext()返回null。

0 个答案:

没有答案