Search code examples
asp.netasp.net-mvcdependency-injectionasp.net-identityninject

IDataProtectionProvider Inject With Ninject


I have implemented Asp.Net Identity ApplicationManager with ninject. I reference this link How to inject UserManager & SignInManager when i imlepentation my code.

But when i try to reset my password it raise "No IUserTokenProvider is registered". Because my IdentityFactoryOptions is null.

How can i inject it to Ninject?

The problem is dataProtectionProvider is always 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"));
        }

    }

Solution

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