微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

覆盖SignInManager在启动时引发错误

如何解决覆盖SignInManager在启动时引发错误

我试图用CustomSignManager覆盖SignInManager,但是在启动类中注册时却出现错误

system.invalidOperationException:类型CustomSignInManager`1必须从UserManager派生。 在Microsoft.AspNetCore.Identity.IdentityBuilder.AddUserManagerTUserManager

下面是我用来注册CustomSignInManager的代码

        services.AddIdentity<ApplicationUser,IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders()
                .AddUserManager<CustomSignInManager<ApplicationUser>>(); 


services.AddScoped<SignInManager<ApplicationUser>,CustomSignInManager<ApplicationUser>>();

在CustomSignInManager上,我只是返回要返回的任何基类,以使此处变得简单。

public class CustomSignInManager<TUser> : SignInManager<TUser> where TUser : class
{
    public CustomSignInManager(UserManager<TUser> userManager,IHttpContextAccessor contextAccessor,IUserClaimsPrincipalFactory<TUser> claimsFactory,IOptions<IdentityOptions> optionsAccessor,ILogger<SignInManager<TUser>> logger,ApplicationDbContext dbContext,IAuthenticationSchemeProvider schemes,IUserConfirmation<TUser> confirmation)
        : base(userManager,contextAccessor,claimsFactory,optionsAccessor,logger,schemes,confirmation)
    {
    }
}

解决方法

System.InvalidOperationException:类型CustomSignInManager`1必须派生自UserManager

在您的代码中,我们发现您想使用ConfigureServices类的Startup方法中的自定义SignInManager配置身份服务,但是您调用.AddUserManager<TUserManager> method来添加自定义SignInManager,这会导致此错误。

.AddUserManager >()

要解决此问题,您可以尝试调用.AddSignInManager<CustomSignInManager<ApplicationUser>>()方法来添加自定义SignInManager,而不使用上面的代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。