我从开箱即用的MVC5安装开始,并将AutoFac用作IoC容器.听起来我想要达到和你类似的目标,所以让我解释一下我都做了些什么.作为免责声明,我对使用IOC和身份认同相当陌生.
如果您使用自己的IOC,我认为IOwinContext在IOC角色中是不必要的-我切换到使用AutoFac注册我的ApplicationUserManager.要做到这一点,我必须:
从启动中删除CreateWinContext行.验证,因为我将在AutoFac中注册ApplicationDbContext
和ApplicationUserManager
.
//app.CreatePerOwinContext(ApplicationDbContext.Create);
//app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
修改ApplicationUserManager构造函数参数,包括Create函数中的所有内容.
public ApplicationUserManager(IUserStore<ApplicationUser> store, IdentityFactoryOptions<ApplicationUserManager> options)
: base(store)
{
//all the code from the 'Create' function here, using `this` for `manager`
}
将AccountController设置为有一个以ApplicationUserManager
为参数的构造函数,并放弃从OwinContext
中获取ApplicationUserManager
的UserManager
属性.
private ApplicationUserManager _userManager; //every thing that needs the old UserManager property references this now
public AccountController(ApplicationUserManager userManager)
{
_userManager = userManager;
}
向AutoFac注册所有内容,包括IdentityFactoryOptions的实例.
var x = new ApplicationDbContext();
builder.Register<ApplicationDbContext>(c => x);
builder.Register<UserStore<ApplicationUser>>(c => new UserStore<ApplicationUser>(x)).AsImplementedInterfaces();
builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => new IdentityFactoryOptions<ApplicationUserManager>()
{
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
builder.RegisterType<ApplicationUserManager>();
这是粗略的总结.我可能错过了一路上必须做的其他一些调整.