2017-07-18 99 views
2

我在拨打_userManager.GenerateEmailConfirmationTokenAsync(user.Id);的电话时遇到了错误No IUserTokenProvider is registered,该帐户正在生成一个令牌,并在帐户注册电子邮件中发送。我已经审查了许多与此相关的帖子,但都没有解决我的问题。从我了解到,这个功能是由下面挂接在ApplicationUserManager类:ASP.NET MVC:没有使用Ninject注册IUserTokenProvider

if (dataProtectionProvider != null) 
{ 
    IDataProtector dataProtector = dataProtectionProvider.Create("ASP.NET Identity"); 

    this.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtector); 
} 

我试图解决通过执行以下操作的问题,因为其他地方的建议:我ApplicationUserManager类具有下列德签名: public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider)dataProtectionProvider我使其喷出通过Ninject在Startup.cs必然是这样的:

private IAppBuilder _app; 

    public void Configuration(IAppBuilder app) 
    { 
     _app = app; 
     ConfigureAuth(app); 
     app.UseNinjectMiddleware(CreateKernel); 
    } 

    private IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     kernel.Load(Assembly.GetExecutingAssembly()); 

     //bindings 
     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     kernel.Bind<DbContext>().To<MvcIndividualAuthContext>().InRequestScope(); 
     kernel.Bind(typeof(IUserStore<>)).To(typeof(UserStore<>)).InRequestScope(); 
     kernel.Load(Assembly.GetExecutingAssembly()); 
     kernel.Bind<MvcIndividualAuthContext>().ToSelf().InRequestScope(); 
     kernel.Bind<IUserStore<ApplicationUser, string>>().To<ApplicationUserStore>(); 
     kernel.Bind<ApplicationUserManager>().ToSelf(); 
     kernel.Bind<ApplicationSignInManager>().ToSelf(); 
     kernel.Bind<IAuthenticationManager>().ToMethod(x => HttpContext.Current.GetOwinContext().Authentication); 
     kernel.Bind<IdentityFactoryOptions<ApplicationUserManager>>().ToSelf(); 

     //this bind should be binding the IDataProtectionProvider for my 
     //ApplicationUserManager 
     kernel.Bind<IDataProtectionProvider>().ToMethod(x => _app.GetDataProtectionProvider()); 


     return kernel; 
    } 

但是绑定似乎并不奏效,因为我ApplicationUserManager在生成我的令牌时,UserTokenProvider仍为空。作为参考,你可以找到的代码为我ApplicationUserManager如下:

public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
    public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider) 
     : base(store) 
    { 
     // Configure validation logic for usernames 
     this.UserValidator = new UserValidator<ApplicationUser>(this) 
     { 
     AllowOnlyAlphanumericUserNames = false, 
     RequireUniqueEmail = true 
     }; 

     // Configure validation logic for passwords 
     this.PasswordValidator = new PasswordValidator 
     { 
     RequiredLength = 6, 
     RequireNonLetterOrDigit = true, 
     RequireDigit = true, 
     RequireLowercase = true, 
     RequireUppercase = true, 
     }; 

     // Configure user lockout defaults 
     this.UserLockoutEnabledByDefault = true; 
     this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
     this.MaxFailedAccessAttemptsBeforeLockout = 5; 

     // 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 it in here. 
     this.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> 
     { 
     MessageFormat = "Your security code is {0}" 
     }); 
     this.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> 
     { 
     Subject = "Security Code", 
     BodyFormat = "Your security code is {0}" 
     }); 
     this.EmailService = new EmailService(); 
     this.SmsService = new SmsService(); 

     if (dataProtectionProvider != null) 
     { 
     IDataProtector dataProtector = dataProtectionProvider.Create("ASP.NET Identity"); 

     this.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtector); 
     } 
    } 
    } 

所有帮助是极大的赞赏。

+0

是否https://stackoverflow.com/questions/27471363/no-iusertokenprovider-is-registered-when-using-structuremap-dependency-injecti或https://stackoverflow.com/questions/30474214/no-iusertokenprovider-注册时使用依赖注入帮助? – mjwills

+0

IDataProtectionProvider被注入到我的'ApplicationUserManager'中,这个IDataProtectionProvider通过构造函数'IDataProtector dataProtector = dataProtectionProvider.Create(“ASP.NET Identity”);'this.UserTokenProvider = new DataProtectorTokenProvider'中的以下两行提供'IUserTokenProvider' (dataProtector);'。所述结合被登记在与Startup.cs'kernel.Bind ()ToMethod(X => _app.GetDataProtectionProvider());' – GregH

+0

@mjwills unforuntately不,我现在已经无数次审查这两个柱的和是试图使用他们的解决方案无济于事 – GregH

回答

0

清洁和构建解决方案后,问题已得到解决。请注意,解决方案在发生问题期间已经建立了多次,但未清理。