gpt4 book ai didi

c# - ASP.NET MVC : No IUserTokenProvider is registered using Ninject

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:05 25 4
gpt4 key购买 nike

我遇到了错误 No IUserTokenProvider is registered调用 _userManager.GenerateEmailConfirmationTokenAsync(user.Id);它正在生成要在帐户注册电子邮件中发送的 token 。我查看了许多与此相关的帖子,但没有一个能解决我的问题。据我了解,此功能与 ApplicationUserManager 相关联。按以下分类:

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

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

我已尝试按照其他地方的建议执行以下操作来解决问题:我的 ApplicationUserManager类具有以下签名: public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider)dataProtectionProvider我正在注入(inject)由 Ninject 在 Startup.cs 中绑定(bind)像这样:

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

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

非常感谢所有帮助。

最佳答案

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

关于c# - ASP.NET MVC : No IUserTokenProvider is registered using Ninject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com