本文介绍了使用自定义SignInManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置身份如下:
services.AddIdentity<IdentityUser, IdentityRole>(
c =>
{
c.Password.RequireDigit = false;
c.Password.RequiredLength = 8;
c.Password.RequireLowercase = false;
c.Password.RequireNonLetterOrDigit = false;
c.Password.RequireUppercase = false;
})
.AddUserManager<CustomUserManager>()
.AddUserValidator<CustomUserValidator>()
.AddCustomStores<PrimaryContext>()
.AddDefaultTokenProviders();
我想使用自定义登录管理器,但是没有AddSignInManager
方法.我看到其他人建议我在上面的片段下面添加以下行:
I'd like to use a custom sign-in manager, but there is no AddSignInManager
method. I have seen others suggest I add the following line below the fragment above:
services.AddScoped<SignInManager<IdentityUser>, CustomSignInManager>();
但是,这会导致内部服务器错误:
However, this causes an internal server error:
InvalidOperationException: Unable to resolve service for type 'MyProject.Identity.CustomSignInManager' while attempting to activate 'MyProject.Controllers.AccountController'.
如何使我的自定义登录管理器正常工作?
How can I get my custom sign-in manager working?
推荐答案
原来是我的误解.而不是在我的控制器中执行以下操作:
Turned out to be a misunderstanding on my part. Rather than the following in my controller:
public AccountController(CustomUserManager userManager,
CustomSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
这个更冗长的代码块似乎可以与我原始问题中显示的其他行结合使用:
This more verbose block of code appears to work in combination with the additional line shown in my original question:
public AccountController(CustomUserManager userManager,
SignInManager<IdentityUser> signInManager)
{
if(!(signInManager is CustomSignInManager))
{
throw new ArgumentException(
"signInManager must be an instance of CustomSignInManager");
}
UserManager = userManager;
SignInManager = signInManager as CustomSignInManager;
}
这不是特别好,但是可以.
This isn't especially nice but it works.
这篇关于使用自定义SignInManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!