2

I am running into the error No IUserTokenProvider is registered on a call to _userManager.GenerateEmailConfirmationTokenAsync(user.Id); which is generating a token to be sent in an account registration e-mail. I have reviewed many posts related to this and none have solved my issue. From what I've learned, this functionality is hooked up in the ApplicationUserManager class by the following:

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

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

I've attempted to resolve the issue by doing the following as suggested elsewhere: My ApplicationUserManager class has teh following signature: public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider) and the dataProtectionProvider I am injecting is bound by Ninject in Startup.cs like this:

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

however the binding doesn't seem to be working because my ApplicationUserManager's UserTokenProvider is still null at the time of generating my token. For reference, you can find the code for my ApplicationUserManager below:

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

All help is greatly appreciated.

GregH
  • 5,125
  • 8
  • 55
  • 109
  • Does https://stackoverflow.com/questions/27471363/no-iusertokenprovider-is-registered-when-using-structuremap-dependency-injecti or https://stackoverflow.com/questions/30474214/no-iusertokenprovider-is-registered-when-using-dependency-injection help? – mjwills Jul 18 '17 at 13:38
  • IDataProtectionProvider is injected into my `ApplicationUserManager` and this IDataProtectionProvider provides the `IUserTokenProvider` by teh following two lines in the constructor `IDataProtector dataProtector = dataProtectionProvider.Create("ASP.NET Identity");` and `this.UserTokenProvider = new DataProtectorTokenProvider(dataProtector);`. The binding is registered in Startup.cs with `kernel.Bind().ToMethod(x => _app.GetDataProtectionProvider());` – GregH Jul 18 '17 at 13:39
  • @mjwills unforuntately not, I have reviewed both of those posts numerous times now and am trying to use their solutions to no avail – GregH Jul 18 '17 at 13:40

1 Answers1

0

After cleaning and building the solution, the issue has been resolved. Note that the solution had been built numerous times during while the issue was occurring but not cleaned.

GregH
  • 5,125
  • 8
  • 55
  • 109