IDataProtectionProvider使用Ninject注入

时间:2018-11-06 22:19:39

标签: asp.net asp.net-mvc dependency-injection asp.net-identity ninject

我已经用ninject实现了Asp.Net Identity ApplicationManager。当我植入我的代码时,我会引用此链接How to inject UserManager & SignInManager

但是当我尝试重置密码时,它会提示“未注册IUserTokenProvider”。 因为我的IdentityFactoryOptions为null。

如何将其注入Ninject?

问题是dataProtectionProvider始终为空

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

    }

1 个答案:

答案 0 :(得分:0)

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