简体   繁体   中英

Creating a custom SignInManager in Asp.Net Core 3 Identity

I want to create a custom class for my SignInManager, so I've created a class that inherts from SignInManager<> as follows:

public class ApplicationSignInManager : SignInManager<ApplicationUser>
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly ApplicationDbContext _dbContext;
    private readonly IHttpContextAccessor _contextAccessor;

    public ApplicationSignInManager(
UserManager<ApplicationUser> userManager,
        IHttpContextAccessor contextAccessor,
        IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory,
        IOptions<IdentityOptions> optionsAccessor,
        ILogger<SignInManager<ApplicationUser>> logger,
        ApplicationDbContext dbContext,
        IAuthenticationSchemeProvider schemeProvider
        )
        : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemeProvider)
    {
        _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
        _contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
        _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
    }
}

and then I've added it in the services configuration in Startup.cs :

services.AddDefaultIdentity<ApplicationUser>(configure =>
{
    configure.User.AllowedUserNameCharacters += " ";
}).AddSignInManager<ApplicationSignInManager>()
  .AddDefaultUI(UIFramework.Bootstrap4)
  .AddEntityFrameworkStores<ApplicationDbContext>();

The problem is that the default SignInManager<ApplicationUser> cannot be casted to a ApplicationSignInManager , so I get this error when accessing a page in whose controller the manager is injected:

InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Identity.SignInManager`1[Socialize.Data.ApplicationUser]' to type 'Socialize.Utilities.Identity.ApplicationSignInManager' .

Your issue is caused by that you register AddSignInManager<ApplicationSignInManager>() before .AddDefaultUI(UIFramework.Bootstrap4) .

For AddDefaultUI , it will call builder.AddSignInManager(); which will register the typeof(SignInManager<>).MakeGenericType(builder.UserType) and will override your previous settings.

Try Code below:

        services.AddDefaultIdentity<ApplicationUser>()                
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddSignInManager<ApplicationSignInManager>();

In.Net Core 6

 public ApplicationSignInManager(
    UserManager<IdentityUser> userManager,
            IHttpContextAccessor contextAccessor,
            IUserClaimsPrincipalFactory<IdentityUser> claimsFactory,
            IOptions<IdentityOptions> optionsAccessor,
            ILogger<SignInManager<IdentityUser>> logger,
            ApplicationDbContext context,
            IAuthenticationSchemeProvider schemeProvider,
            IUserConfirmation<IdentityUser> iUserConfirmation
            )
            :base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemeProvider, iUserConfirmation)
    {
        _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
        _contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM