9

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'.

2
  • What is your controller code? Fail to reproduce your issue. You need to inject ApplicationSignInManager instead of SignInManager<ApplicationUser>. Commented Mar 19, 2019 at 6:24
  • I had already injected ApplicationSIgnInManager. It was fixed by your other answer. Commented Mar 19, 2019 at 11:41

3 Answers 3

15

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>();
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me without having to Inject ApplicationSignInManager into my controllers. They still worked fine with the standard SignInManager injected.
0

In this resource, you can use your own user tables without identity tables (AspNet*).

https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/authentication/identity-custom-storage-providers/sample/CustomIdentityProviderSample

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.