IDataProtectionProvider 注入 Ninject

IDataProtectionProvider Inject With Ninject

我已经使用 ninject 实现了 Asp.Net Identity ApplicationManager。当我实现我的代码时,我引用了这个 link

但是当我尝试重设密码时,它出现了 "No IUserTokenProvider is registered"。 因为我的 IdentityFactoryOptions 是空的。

如何将其注入 Ninject?

问题是 dataProtectionProvider 始终为 null

    if (dataProtectionProvider != null)
    {
        this.UserTokenProvider =
            new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
    }



    private static void RegisterServices(IKernel kernel)
    {
         ...
        kernel.Bind<IUserStore<ApplicationUser,int>>().To<ApplicationUserStore>();
        kernel.Bind<UserManager<ApplicationUser,int>>().ToSelf();

        kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();

        kernel.Bind<ApplicationSignInManager>().ToMethod((context) =>
        {
            var cbase = new HttpContextWrapper(HttpContext.Current);
            return cbase.GetOwinContext().Get<ApplicationSignInManager>();
        });

        kernel.Bind<ApplicationUserManager>().ToSelf();
        kernel.Bind<IUserService>().To<ApplicationUserManager>().InRequestScope();
        ...
}

 public ApplicationUserManager(ApplicationUserStore store, 
                        IdentityFactoryOptions<ApplicationUserManager> options) : base(store)
    {
        _store = store;

        this.UserValidator = new UserValidator<ApplicationUser, int>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = false,
        };

        // Configure validation logic for passwords
        this.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 4,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };
        // Configure user lockout defaults
        this.UserLockoutEnabledByDefault = true;
        this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        this.MaxFailedAccessAttemptsBeforeLockout = 10;
        // 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.
        this.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser, int>
        {
            MessageFormat = "Your security code is: {0}"
        });
        this.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser, int>
        {
            Subject = "SecurityCode",
            BodyFormat = "Your security code is {0}"
        });


        var dataProtectionProvider = options.DataProtectionProvider;

        //dataProtectionProvider is always null
        if (dataProtectionProvider != null)
        {
            this.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
        }

    }
var dataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("MyAppName");