配置统一DI为ASP.NET身份 [英] Configure Unity DI for ASP.NET Identity
问题描述
我使用Unity成功为所有常规构造注射,如仓库等,但我不能让它与ASP.NET身份类的工作。该设置是这样的:
公共类的AccountController:ApiController
{
私人的UserManager< ApplicationUser> _userManager {搞定;组; } 公众的AccountController(的UserManager< ApplicationUser>的UserManager)
{
如果(的UserManager == NULL){抛出新的ArgumentNullException(的UserManager); }
_userManager =的UserManager;
} // ...
}
这些CONFIGS团结:
unity.RegisterType<&的AccountController GT;();
unity.RegisterType&所述;的UserManager&下; ApplicationUser>>(新HierarchicalLifetimeManager());
unity.RegisterType&所述; IUserStore&下; ApplicationUser>中UserStore&所述; ApplicationUser>>(新HierarchicalLifetimeManager());
这是一样在这里的其他职位Autofac,Ninject例如,但它并不在我的情况下工作。错误消息是:
试图创建类型的控制器的AccountController时发生错误。确保控制器具有一个无参数公共构造函数。
手动创作的作品,当然:
块引用>公开的AccountController()
:这(新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(蒙戈)))
{
}怎么了?
更新
由于建议的@emodendroket,缩短了code到这样做的伎俩。不需要对Unity.Mvc包
unity.RegisterType< IUserStore< IdentityUser>中
MyCustomUserStore>(新HierarchicalLifetimeManager());和
公开的AccountController(的UserManager< IdentityUser> _userManager,
IAccountRepository _account)
{
// ...
解决方案您还需要解决在的UserManager 。下面是一个例子中,你如何与在的UserManager 做到这一点,在 RoleManager 。在这个示例中,我使用常规的统一3包,而不是衍生物或bootstrappers之一(在过去的一些问题与他们)。
的AccountController
私人只读的UserManager< ApplicationUser> _userManager;私人只读RoleManager< IdentityRole> _roleManager;公众的AccountController(IUserStore< ApplicationUser> userStore,IRoleStore< IdentityRole> Rolestore的)
{
_userManager =新的UserManager< ApplicationUser>(userStore);
_roleManager =新RoleManager< IdentityRole>(Rolestore的);
}团结引导程序
VAR accountInjectionConstructor =新InjectionConstructor(新IdentitySampleDbModelContext(configurationStore));
container.RegisterType&所述; IUserStore&下; ApplicationUser>中UserStore&所述; ApplicationUser>>(accountInjectionConstructor);
container.RegisterType&所述; IRoleStore&下; IdentityRole>中Rolestore的&所述; IdentityRole>>(accountInjectionConstructor);I'm using Unity successfully for all regular constructor injection such as repositories etc., but I can't get it working with the ASP.NET Identity classes. The setup is this:
public class AccountController : ApiController { private UserManager<ApplicationUser> _userManager { get; set; } public AccountController(UserManager<ApplicationUser> userManager) { if (userManager == null) { throw new ArgumentNullException("userManager"); } _userManager = userManager; } // ... }
with these configs for Unity:
unity.RegisterType<AccountController>(); unity.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager()); unity.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
That's the same as other posts here for Autofac, Ninject e.g., but it doesn't work in my case. The error message is:
An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor. Manual creation works of course:
public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>("Mongo"))) { }
What's wrong?
UPDATE
As suggested by @emodendroket, shortening the code to this does the trick. No need for the Unity.Mvc package.
unity.RegisterType<IUserStore<IdentityUser>, MyCustomUserStore>(new HierarchicalLifetimeManager());
and
public AccountController(UserManager<IdentityUser> _userManager, IAccountRepository _account) { // ...
解决方案You also need to resolve the UserManager. The following is an example how you could do it with the UserManager and the RoleManager. In this sample I use the regular Unity 3 package instead of one of the derivates or bootstrappers (had some problems with them in the past).
AccountController
private readonly UserManager<ApplicationUser> _userManager; private readonly RoleManager<IdentityRole> _roleManager; public AccountController(IUserStore<ApplicationUser> userStore, IRoleStore<IdentityRole> roleStore) { _userManager = new UserManager<ApplicationUser>(userStore); _roleManager = new RoleManager<IdentityRole>(roleStore); }
Unity Bootstrapper
var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore)); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(accountInjectionConstructor); container.RegisterType<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>(accountInjectionConstructor);
这篇关于配置统一DI为ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!