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

Ray*_*Ray 5 c# asp.net identityserver4

我一直在尝试将 Identity Server 4 合并到我的 ASP.NET Core 3 应用程序中,但不断收到以下错误:

没有为方案“Identity.Application”注册登录身份验证处理程序。

注册的登录方案有: Cookie。你忘记打电话了吗AddAuthentication().AddCookies("Identity.Application",...)?我不确定这个错误是什么意思。

我查看了这个SO问题,这篇(在ASP.NET Core中使用特定方案授权) MS .NET文章以及其他几篇文章,但没有一个有帮助。

我的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
    // /sf/ask/3113851261/#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);
}
Run Code Online (Sandbox Code Playgroud)

我的控制器包含:

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.") }));
}
Run Code Online (Sandbox Code Playgroud)

执行时出现错误_signInMgr.PasswordSignInAsync(model.Email, model.Password, false, false)

Den*_* W. 7

发生这种情况是因为.AddIdentityCore()没有为您配置 cookie。您需要致电.AddIdentity()使其自动设置。否则,你必须自己做。

        .AddAuthentication(x =>
        {
            x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) 
        .AddCookie(IdentityConstants.ApplicationScheme)
Run Code Online (Sandbox Code Playgroud)

编辑:

要了解调用的服务集合扩展方法内部发生了什么,请AddIdentity()查看与此代码相关的GitHub 存储库。