c# – AspIdentiy ApplicationUserManager是静态的,如何扩展,以便它参与我的IoC框架?

前端之家收集整理的这篇文章主要介绍了c# – AspIdentiy ApplicationUserManager是静态的,如何扩展,以便它参与我的IoC框架?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在一个新的ASPNET MVC应用程序中,您现在可以免费得到AspIdentity的好东西.

这里有一个无害的小线’插入你的电子邮件服务’.

所以我做了:

  1. public class EmailService : IIdentityMessageService
  2. {
  3. private static My.Services.IEmailService _emailservice;
  4.  
  5. public EmailService(Insolvency.Services.IEmailService emailservice)
  6. {
  7. _emailservice = emailservice;
  8. }
  9.  
  10. public Task SendAsync(IdentityMessage message)
  11. {
  12. _emailservice.SendEmail(message);
  13. return Task.FromResult(0);
  14. }
  15. }

现在的喜悦:

  1. public class ApplicationUserManager : UserManager<ApplicationUser>
  2. {
  3. private My.Services.IEmailService _emailservice;
  4.  
  5. public ApplicationUserManager(IUserStore<ApplicationUser> store,My.Services.IEmailService emailservice): base(store)
  6. {
  7. _emailservice = emailservice;
  8. }
  9.  
  10. public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,IOwinContext context)
  11. {
  12. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()),_emailservice);
  13. ...

正如Owin在其中启动的那样,在Startup.Auth.cs中调用Create on ApplicationUserManager:

  1. public partial class Startup
  2. {
  3. public void ConfigureAuth(IAppBuilder app)
  4. {
  5. ...
  6. app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

因为我正在使用AutoFac作为我的IoC容器,在global.asax.cs中

  1. builder.RegisterType<My.Services.EmailService>().As<IEmailService>();

如果Create方法是静态的,我得到:
_emailService为空

我看过这里:http://forums.asp.net/post/5293670.aspxHow do i create an instance of UserManagerUsing Autofac to provide types exported by static factory.
但没有运气.

如果我改变:

  1. private My.Services.IEmailService _emailservice;

被公开的非静态我感觉到IoC的神摇头,我不能建立“对象引用必需”

解决方法

今天我正在努力解决同样的问题(自从我刚开始接受Asp.Identity之后,我仍在努力工作).
我这样做:

> Startup.cs(使用你自己的容器)

  1. public class Startup
  2. {
  3. public void Configuration(IAppBuilder app)
  4. {
  5. var authConfigurator = new AuthConfigurator();
  6. authConfigurator.ConfigureAuth(app);
  7. var unityContainer = UnityConfig<MvcUnityDependencyContainer>.UseContainer();
  8. //Asp identity registration
  9. IDataProtectionProvider dataProtectionProvider = app.GetDataProtectionProvider();
  10. unityContainer.RegisterInstance(dataProtectionProvider);
  11. unityContainer.RegisterType<DbContext,ApplicationDbContext>(new HierarchicalLifetimeManager());
  12. unityContainer.RegisterType<UserManager<ApplicationUser,int>>(new HierarchicalLifetimeManager());
  13. unityContainer.RegisterType IIdentityMessageService,EmailService>();
  14. unityContainer.RegisterType<IUserStore<ApplicationUser,int>,UserStore<ApplicationUser,CustomRole,int,CustomUserLogin,CustomUserRole,CustomUserClaim>>(
  15. new InjectionConstructor(new ApplicationDbContext()));
  16. unityContainer.RegisterType<IIdentityMessageService,EmailService>();
  17. }
  18. }

> ApplicationUserManager(我删除静态方法Create):

  1. public class ApplicationUserManagerService : UserManager<ApplicationUser,int>
  2. {
  3. public ApplicationUserManagerService(IUserStore<ApplicationUser,int> store,IIdentityMessageService emailService,IDataProtectionProvider dataProtectionProvider)
  4. : base(store)
  5. {
  6. UserTokenProvider = new EmailTokenProvider<ApplicationUser,int>();
  7. EmailService = emailService;
  8. Configure(dataProtectionProvider);
  9. }
  10. private void Configure(IDataProtectionProvider dataProtectionProvider)
  11. {
  12. // Configure validation logic for usernames
  13. UserValidator = new UserValidator<ApplicationUser,int>(this)
  14. {
  15. AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true
  16. };
  17. // Configure validation logic for passwords
  18. PasswordValidator = new PasswordValidator
  19. {
  20. requiredLength = 1,RequireNonLetterOrDigit = false,requiredigit = false,RequireLowercase = false,RequireUppercase = false,};
  21. // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
  22. // You can write your own provider and plug in here.
  23. RegisterTwoFactorProvider("PhoneCode",new PhoneNumberTokenProvider<ApplicationUser,int>
  24. {
  25. MessageFormat = "Your security code is: {0}"
  26. });
  27. RegisterTwoFactorProvider("EmailCode",new EmailTokenProvider<ApplicationUser,int>
  28. {
  29. Subject = "Security Code",BodyFormat = "Your security code is: {0}"
  30. });
  31. if (dataProtectionProvider != null)
  32. {
  33. UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser,int>(dataProtectionProvider.Create("ASP.NET Identity"));
  34. }
  35. }
  36. }

>控制器

  1. public class AccountController : Controller
  2. {
  3. private ApplicationUserManagerService _userManagerService;
  4. public AccountController(ApplicationUserManagerService userManagerService)
  5. {
  6. Contract.Requires(userManagerService != null);
  7. _userManagerService = userManagerService;
  8. }
  9. /*....*/
  10. }

> ApplicationUser

  1. public class ApplicationUser : IdentityUser<int,CustomUserClaim>
  2. {
  3. public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,int> manager)
  4. {
  5. var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
  6. return userIdentity;
  7. }
  8. }
  9.  
  10. public class CustomRole : IdentityRole<int,CustomUserRole>
  11. {
  12. public CustomRole() { }
  13. public CustomRole(string name) { Name = name; }
  14. }
  15.  
  16. public class CustomUserClaim : IdentityUserClaim<int> { }
  17. public class CustomUserLogin : IdentityUserLogin<int> { }
  18. public class CustomUserRole : IdentityUserRole<int> { }

这适用于我,但请随时提出更好的解决方案.

猜你在找的C#相关文章