C# Aspintiy ApplicationUserManager是静态的,如何扩展以使其参与我的IoC框架?
在新的ASPNET MVC应用程序中,您现在可以免费获得AspIdentity商品 这里有一句无害的话“在这里插入你的电子邮件服务” 所以我做了:C# Aspintiy ApplicationUserManager是静态的,如何扩展以使其参与我的IoC框架?,c#,asp.net,asp.net-mvc,mocking,C#,Asp.net,Asp.net Mvc,Mocking,在新的ASPNET MVC应用程序中,您现在可以免费获得AspIdentity商品 这里有一句无害的话“在这里插入你的电子邮件服务” 所以我做了: public class EmailService : IIdentityMessageService { private static My.Services.IEmailService _emailservice; public EmailService(Insolvency.Services.IEmailService ema
public class EmailService : IIdentityMessageService
{
private static My.Services.IEmailService _emailservice;
public EmailService(Insolvency.Services.IEmailService emailservice)
{
_emailservice = emailservice;
}
public Task SendAsync(IdentityMessage message)
{
_emailservice.SendEmail(message);
return Task.FromResult(0);
}
}
现在是快乐:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
private My.Services.IEmailService _emailservice;
public ApplicationUserManager(IUserStore<ApplicationUser> store, My.Services.IEmailService emailservice): base(store)
{
_emailservice = emailservice;
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()), _emailservice);
...
作为一个非静态的公众,我感觉到IoC诸神在摇头,我无法构建“需要对象引用”今天我也在为同样的问题而挣扎(因为我是Asp.Identity的新手,所以仍在努力)。 我是这样做的:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var authConfigurator = new AuthConfigurator();
authConfigurator.ConfigureAuth(app);
var unityContainer = UnityConfig<MvcUnityDependencyContainer>.UseContainer();
//Asp identity registration
IDataProtectionProvider dataProtectionProvider = app.GetDataProtectionProvider();
unityContainer.RegisterInstance(dataProtectionProvider);
unityContainer.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
unityContainer.RegisterType<UserManager<ApplicationUser, int>>(new HierarchicalLifetimeManager());
unityContainer.RegisterType IIdentityMessageService, EmailService>();
unityContainer.RegisterType<IUserStore<ApplicationUser, int>,
UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>>(
new InjectionConstructor(new ApplicationDbContext()));
unityContainer.RegisterType<IIdentityMessageService, EmailService>();
}
}
public class AccountController : Controller
{
private ApplicationUserManagerService _userManagerService;
public AccountController(ApplicationUserManagerService userManagerService)
{
Contract.Requires(userManagerService != null);
_userManagerService = userManagerService;
}
/*....*/
}
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomUserRole : IdentityUserRole<int> { }
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
返回用户身份;
}
}
公共类CustomRole:IdentityRole
{
公共CustomRole(){}
公共自定义角色(字符串名称){name=name;}
}
公共类CustomUserClaim:IdentityUserClaim{}
公共类CustomUserLogin:IdentityUserLogin{}
公共类CustomUserRole:IdentityUserRole{}
这对我来说很有效,但请随意提出更好的解决方案。我今天为此奋斗了很长一段时间,最后,最简单的方法就是这样做 内部
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
把它改成这个
manager.EmailService = new EmailService(DependencyResolver.Current.GetService<Insolvency.Services.IEmailService>());
manager.EmailService=新的EmailService(DependencyResolver.Current.GetService());
我不喜欢服务定位器模式,但所有MVC模板化的代码都使用它,最好不要与框架对抗。使用Unity,我在UnityConfig.cs中的注册类型(IUnityContainer容器)中添加了以下内容:
container.RegisterType<ApplicationUserManager>(New InjectionFactory(x => HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()));
container.RegisterType(新注入工厂(x=>HttpContext.Current.GetOwinContext().GetUserManager());
这将获取已在
Startup.ConfigureAuth(IAppBuilder应用程序)
中为OwinContext创建的ApplicationUserManager,并使其可用于注入。您可以将其他所有内容保留在默认设置中,包括ApplicationUserManager的静态创建方法。谢谢。我将尝试在Unity和Autofac之间转换。当前编译器给出错误:。。。类型“ApplicationUser”不能用作泛型类型或方法“Microsoft.AspNet.Identity.EmailTokenProvider”中的类型参数“TUser”。没有从“ApplicationUser”到“Microsoft.AspNet.Identity.IUser”的隐式引用转换。您能提供IdentityModels的来源吗?csToday用于AspIdentity 3的RC具有我们在中讨论过的所有DI优点。您从哪里获得AuthConfigurator?@free4ride您是如何设置控制器的?我一直在尝试创建“MyController”类型的控制器时出错。确保控制器具有无参数公共构造函数。当我尝试添加DBContext时,总是发生这种情况
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomUserRole : IdentityUserRole<int> { }
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
manager.EmailService = new EmailService(DependencyResolver.Current.GetService<Insolvency.Services.IEmailService>());
container.RegisterType<ApplicationUserManager>(New InjectionFactory(x => HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()));