ASP.NET MVC:没有使用 Ninject 注册 IUserTokenProvider

ASP.NET MVC: No IUserTokenProvider is registered using Ninject

我在调用 _userManager.GenerateEmailConfirmationTokenAsync(user.Id); 时 运行 遇到错误 No IUserTokenProvider is registered,它正在生成要在帐户注册电子邮件中发送的令牌。我查看了许多与此相关的帖子,none 解决了我的问题。据我所知,此功能通过以下方式连接到 ApplicationUserManager class 中:

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

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

我已尝试按照其他地方的建议执行以下操作来解决此问题:我的 ApplicationUserManager class 具有以下签名: public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider) 我正在注入的 dataProtectionProviderStartup.cs 中的 Ninject 约束,如下所示:

    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;
    }

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

非常感谢所有帮助。

清理并构建解决方案后,问题已解决。请注意,该解决方案在问题出现但未清除期间已构建多次。