这里有一个无害的小线’插入你的电子邮件服务’.
所以我做了:
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); ...
正如Owin在其中启动的那样,在Startup.Auth.cs中调用Create on ApplicationUserManager:
public partial class Startup { public voID ConfigureAuth(IAppBuilder app) { ... app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
因为我正在使用autoFac作为我的IoC容器,在global.asax.cs中
builder.RegisterType<My.Services.EmailService>().As<IEmailService>();
如果Create方法是静态的,我得到:
_emailService为空
我看过这里:http://forums.asp.net/post/5293670.aspx和How do i create an instance of UserManager和Using Autofac to provide types exported by static factory.
但没有运气.
如果我改变:
private My.Services.IEmailService _emailservice;
被公开的非静态我感觉到IoC的神摇头,我不能建立“对象引用必需”
解决方法 今天我正在努力解决同样的问题(自从我刚开始接受Asp.IDentity之后,我仍在努力工作).我这样做:
> Startup.cs(使用你自己的容器)
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>(); }}
> ApplicationUserManager(我删除静态方法Create):
public class ApplicationUserManagerService : UserManager<ApplicationUser,int>{ public ApplicationUserManagerService(IUserStore<ApplicationUser,int> store,IIDentityMessageService emailService,IDataProtectionProvIDer dataProtectionProvIDer) : base(store) { UserTokenProvIDer = new EmailTokenProvIDer<ApplicationUser,int>(); EmailService = emailService; Configure(dataProtectionProvIDer); } private voID Configure(IDataProtectionProvIDer dataProtectionProvIDer) { // Configure valIDation logic for usernames UserValIDator = new UserValIDator<ApplicationUser,int>(this) { AllowOnlyAlphanumericUsernames = false,RequireUniqueEmail = true }; // Configure valIDation logic for passwords PasswordValIDator = new PasswordValIDator { requiredLength = 1,RequireNonLetterOrDigit = false,requiredigit = false,RequireLowercase = false,RequireUppercase = false,}; // Register two factor authentication provIDers. This application uses Phone and Emails as a step of receiving a code for verifying the user // You can write your own provIDer and plug in here. RegisterTwoFactorProvIDer("PhoneCode",new PhoneNumberTokenProvIDer<ApplicationUser,int> { messageformat = "Your security code is: {0}" }); RegisterTwoFactorProvIDer("EmailCode",new EmailTokenProvIDer<ApplicationUser,int> { Subject = "Security Code",BodyFormat = "Your security code is: {0}" }); if (dataProtectionProvIDer != null) { UserTokenProvIDer = new DataProtectorTokenProvIDer<ApplicationUser,int>(dataProtectionProvIDer.Create("ASP.NET IDentity")); } }}
>控制器
public class AccountController : Controller{ private ApplicationUserManagerService _userManagerService; public AccountController(ApplicationUserManagerService userManagerService) { Contract.Requires(userManagerService != null); _userManagerService = userManagerService; } /*....*/}
> ApplicationUser
public class ApplicationUser : IDentityUser<int,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> { }
这适用于我,但请随时提出更好的解决方案.
总结以上是内存溢出为你收集整理的c# – AspIdentiy ApplicationUserManager是静态的,如何扩展,以便它参与我的IoC框架?全部内容,希望文章能够帮你解决c# – AspIdentiy ApplicationUserManager是静态的,如何扩展,以便它参与我的IoC框架?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)