ASP.NET MVC:没有使用Ninject

时间:2017-07-18 13:32:42

标签: c# asp.net asp.net-mvc ninject ninject.web.mvc

我在调用No IUserTokenProvider is registered时遇到错误_userManager.GenerateEmailConfirmationTokenAsync(user.Id);,该ApplicationUserManager生成将在帐户注册电子邮件中发送的令牌。我已经审查了许多与此相关的帖子,但没有一个解决了我的问题。根据我的经验,此功能通过以下内容连接到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 我注射的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; } 中的Ninject约束:

ApplicationUserManager

但是绑定似乎不起作用,因为我的UserTokenProvider的{​​{1}}在生成令牌时仍为空。作为参考,您可以在下面找到我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);
      }
    }
  }

非常感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

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