Using a Custom SignInManager

▼魔方 西西 提交于 2019-12-11 15:00:46

问题


I setup identity as follows:

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();

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?


回答1:


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.




回答2:


As mentioned in the comments you should be using services.AddScoped<SignInManager<IdentityUser>, CustomSignInManager>();

Even if signinmanagers can be changed in controllers it would be considered as an anti-pattern as it will need to be implemented in each possible controller where you will perform a signin.

The reason to use AddScoped is that it will replace the default implementation, which is what most people want to do.



来源:https://stackoverflow.com/questions/52517170/how-to-extend-correctly-signinmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!