title: Creating a custom SignInManager in Asp.Net Core 3 Identity
body: <p>I want to create a custom class for my SignInManager, so I've created a class that inherts from <code>SignInManager&lt;&gt;</code> as follows:</p>
  
  <pre><code>public class ApplicationSignInManager : SignInManager&lt;ApplicationUser&gt;
  {
      private readonly UserManager&lt;ApplicationUser&gt; _userManager;
      private readonly ApplicationDbContext _dbContext;
      private readonly IHttpContextAccessor _contextAccessor;
  
      public ApplicationSignInManager(
  UserManager&lt;ApplicationUser&gt; userManager,
          IHttpContextAccessor contextAccessor,
          IUserClaimsPrincipalFactory&lt;ApplicationUser&gt; claimsFactory,
          IOptions&lt;IdentityOptions&gt; optionsAccessor,
          ILogger&lt;SignInManager&lt;ApplicationUser&gt;&gt; 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));
      }
  }
  </code></pre>
  
  <p>and then I've added it in the services configuration in <code>Startup.cs</code>:</p>
  
  <pre><code>services.AddDefaultIdentity&lt;ApplicationUser&gt;(configure =&gt;
  {
      configure.User.AllowedUserNameCharacters += " ";
  }).AddSignInManager&lt;ApplicationSignInManager&gt;()
    .AddDefaultUI(UIFramework.Bootstrap4)
    .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  </code></pre>
  
  <p>The problem is that the default <code>SignInManager&lt;ApplicationUser&gt;</code> cannot be casted to a <code>ApplicationSignInManager</code>, so I get this error when accessing a page in whose controller the manager is injected:</p>
  
  <blockquote>
    <p><em>InvalidCastException:</em> Unable to cast object of type <strong>'Microsoft.AspNetCore.Identity.SignInManager`1[Socialize.Data.ApplicationUser]'</strong> to type <strong>'Socialize.Utilities.Identity.ApplicationSignInManager'</strong>.</p>
  </blockquote>