Microsoft Unity DI with Identity
本文关键字:Identity with DI Unity Microsoft | 更新日期: 2023-09-27 18:09:33
我已经浏览了SO上的所有问题,但似乎无法找出我在这里做错了什么。
我有如下UserService
public class UserService : BaseService
{
private readonly Func<UserManager<ApplicationUser>> _userManagerFactory;
private readonly Func<RoleManager<IdentityRole>> _roleManagerFactory;
public UserService(Func<UserManager<ApplicationUser>> userManagerFactory, Func<RoleManager<IdentityRole>> roleManagerFactory)
{
_userManagerFactory = userManagerFactory;
_roleManagerFactory = roleManagerFactory;
}
在我的帐户控制器中注入如下内容;
public class AccountsController : ApiController
{
[Dependency]
public UserService UserService { get; set; }
现在在我的UnityConfig.cs我设置DI如下;
var userInjectionConstructor = new InjectionConstructor(new MyDataContext());
//container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(userInjectionConstructor);
container.RegisterType<UserManager<ApplicationUser>, ApplicationUserManager>();
container.RegisterType<IRoleStore<IdentityRole, string>, RoleStore<IdentityRole>>(userInjectionConstructor);
其中ApplicationUserManager如下;
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var container = (IUnityContainer)Startup.HttpConfiguration.DependencyResolver.GetService(typeof(IUnityContainer));
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new MyDataContext()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
编译并运行,但是,当它试图解析DI时,我得到了错误;
依赖项解析失败,类型= "Microsoft.AspNet.Identity。"UserManager
1[CCS.Core.ApplicationUser]", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore
1[CCS.Core. cn]ApplicationUser]是一个接口,不能被构造。您是否错过了类型映射?
现在这个问题出现了因为如果我使用下面的;
var userInjectionConstructor = new InjectionConstructor(new MyDataContext());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(userInjectionConstructor);
然后DI工作,然而,没有ApplicationUserManager设置(令牌提供程序等)。
现在是ApplicationUserManager。创建调用是静态的,所以我假设我将不得不将其移动到DI的构造函数中?
谁能解释一下我在这里做错了什么以及如何解决?
有一个其他的问题,这是获得用户的角色名称,无论如何,当试图解决这个问题,我偶然发现这个博客,完美地解释了如何注入http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/。
祝你好运!