简体   繁体   English

ASP.NET Core 3 没有为该方案注册登录管理器

[英]ASP.NET Core 3 No Sign-In Manager Is Registered for the Scheme

I have been trying to incorporate Identity Server 4 into my ASP.NET Core 3 application and continually get the following error:我一直在尝试将 Identity Server 4 合并到我的 ASP.NET Core 3 应用程序中,但不断收到以下错误:

No sign-in authentication handler is registered for the scheme 'Identity.Application.没有为方案“Identity.Application”注册登录身份验证处理程序。

The registered sign-in schemes are: Cookies.注册的登录方案是: Cookies。 Did you forget to call AddAuthentication().AddCookies("Identity.Application",...) ?您是否忘记调用AddAuthentication().AddCookies("Identity.Application",...) and I'm not sure what the error means.我不确定这个错误是什么意思。

I had a look at this SO question, this (Authorize with a specific scheme in ASP.NET Core) MS .NET article, as well as several others, but none have helped.我查看了这个SO question、 this (Authorize with a specific scheme in ASP.NET Core) MS .NET 文章以及其他几篇文章,但都没有帮助。

My Startup.cs :我的Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    var appSettingsSection = Configuration.GetSection("AppSettings");
    services.Configure<AppSettings>(appSettingsSection);

    var appSettings = appSettingsSection.Get<AppSettings>();
    var key = Encoding.ASCII.GetBytes(appSettings.Secret);

    services
        .AddAuthentication(x =>
        {
            x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) // , opt => opt.LoginPath = "/Identity"
        .AddJwtBearer(opt =>
        {
            opt.RequireHttpsMetadata = false;
            opt.SaveToken = true;
            opt.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

    services.AddIdentityCore<IdentityUser>(opt =>
    {
        opt.User.RequireUniqueEmail = true;
        opt.Password.RequireDigit = true;
        opt.Password.RequireLowercase = true;
        opt.Password.RequireUppercase = true;
        opt.Password.RequireNonAlphanumeric = true;
        opt.Password.RequiredLength = 6;
    }).AddEntityFrameworkStores<RbIdentityContext>();

    // == The original "AddIdentity" method automatically added all of the following
    // https://stackoverflow.com/questions/44483589/unable-to-resolve-service-for-type-microsoft-aspnetcore-identity-usermanager-w/48598575/#answer-56551234
    services.AddHttpContextAccessor();
    // Identity services
    services.TryAddScoped<IUserValidator<IdentityUser>, UserValidator<IdentityUser>>();
    services.TryAddScoped<IPasswordValidator<IdentityUser>, PasswordValidator<IdentityUser>>();
    services.TryAddScoped<IPasswordHasher<IdentityUser>, PasswordHasher<IdentityUser>>();
    services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
    services.TryAddScoped<IRoleValidator<IdentityRole>, RoleValidator<IdentityRole>>();
    // No interface for the error describer so we can add errors without rev'ing the interface
    services.TryAddScoped<IdentityErrorDescriber>();
    services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<IdentityUser>>();
    services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<IdentityUser>>();
    services.TryAddScoped<IUserClaimsPrincipalFactory<IdentityUser>, UserClaimsPrincipalFactory<IdentityUser, IdentityRole>>();
    services.TryAddScoped<UserManager<IdentityUser>>();
    services.TryAddScoped<SignInManager<IdentityUser>>();
    services.TryAddScoped<RoleManager<IdentityRole>>();
    // 
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}

My controller contains:我的控制器包含:

public async Task<IActionResult> Login([FromBody]UserModel model)
{
    var result = await _signInMgr.PasswordSignInAsync(model.Email, model.Password, false, false).ConfigureAwait(true);
    if (result.Succeeded)
        return Ok();
    else
        return StatusCode(StatusCodes.Status401Unauthorized, JsonConvert.SerializeObject(new { error = ErrorHelper.SetControllerError("Invalid user name or password.") }));
}

The error occurs when executing _signInMgr.PasswordSignInAsync(model.Email, model.Password, false, false) .执行_signInMgr.PasswordSignInAsync(model.Email, model.Password, false, false)时发生_signInMgr.PasswordSignInAsync(model.Email, model.Password, false, false)

It happens because .AddIdentityCore() doesn't configure the cookies for you.这是因为.AddIdentityCore()没有为你配置 cookie。 You'd need to call .AddIdentity() to have it set up automatically.您需要调用.AddIdentity()来自动设置它。 Otherwise, you have to do it yourself.否则,你必须自己做。

        .AddAuthentication(x =>
        {
            x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) 
        .AddCookie(IdentityConstants.ApplicationScheme)

Edit:编辑:

To see what is happening inside the service collection extension methods called AddIdentity() check out the GitHub repo related to this code.要查看名为AddIdentity()的服务集合扩展方法内部发生了什么,请查看与此代码相关的GitHub 存储库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 5 Web API 没有为方案“cookies”注册登录身份验证处理程序 - ASP.NET Core 5 Web API no sign in authentication handler is registered for the Scheme 'cookies' 如何在ASP.NET Core中处理用于登录SMTP客户端的密码存储? - How do i handle password storage for sign-in to the SMTP client in ASP.NET Core? 如何最好地在ASP.NET Core中实现Google社交登录身份验证? - How best to implement Google social sign-in authentication in ASP.NET Core? ASP.NET MVC Core 1.1.2 登录表单验证错误 - ASP.NET MVC Core 1.1.2 Sign-in Form Validation Error ASP.NET Core MVC AzureAD登录成功后运行代码 - ASP.NET Core MVC AzureAD run code after successful sign-in 使用ASP.NET Core身份验证和基于令牌的身份验证时,为什么要登录新用户? - Why sign-in a new user when using ASP.NET Core Identity and token-based auth? ASP.NET Core:Azure AD B2C自助密码重置(注册/登录策略) - ASP.NET Core: Azure AD B2C self service password reset (sign-up/sign-in policy) 使用现有 ASP.NET MVC 应用程序进行 Google 登录 - Google Sign-in with existing ASP.NET MVC Application "ASP.NET Core 2.1 - 身份验证 cookie 已删除,但用户仍可以登录而不会被重定向到外部登录" - ASP.NET Core 2.1 - Authentication cookie deleted but the user could still login without being redirected to external sign-in Asp.Net Core 6.0:FluentValidation 验证器已注册,但如何注册? - Asp.Net Core 6.0 : FluentValidation validators are registered but how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM