实现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。