C# ASP.NET Core 3未为方案注册登录管理器

C# ASP.NET Core 3未为方案注册登录管理器,c#,asp.net,identityserver4,C#,Asp.net,Identityserver4,我一直试图将Identity Server 4合并到我的ASP.NET Core 3应用程序中,但不断出现以下错误: 没有为方案注册登录身份验证处理程序 'Identity.Application 注册的登录方案为:Cookies。您是否忘记调用AddAuthentication().AddCookies(“Identity.Application”,…)?我不知道这个错误是什么意思 我看了这么一篇问题,MS.NET的文章,以及其他几篇文章,但都没有帮助 MyStartup.cs: public

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

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

注册的登录方案为:Cookies。您是否忘记调用
AddAuthentication().AddCookies(“Identity.Application”,…)
?我不知道这个错误是什么意思

我看了这么一篇问题,MS.NET的文章,以及其他几篇文章,但都没有帮助

My
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);
}
public void配置服务(IServiceCollection服务)
{
var appsetingssection=Configuration.GetSection(“AppSettings”);
services.Configure(应用设置部分);
var appSettings=appSettingsSection.Get();
var key=Encoding.ASCII.GetBytes(appSettings.Secret);
服务
.AddAuthentication(x=>
{
x、 DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
x、 DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)/,opt=>opt.LoginPath=“/Identity”
.AddJwtBearer(opt=>
{
opt.requirehttpsmatadata=false;
opt.SaveToken=true;
opt.TokenValidationParameters=新的TokenValidationParameters
{
ValidateSuersigningKey=true,
IssuerSigningKey=新对称性安全密钥(密钥),
validateisuer=false,
ValidateAudience=false
};
});
服务。AddIdentityCore(选项=>
{
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();
//==原始的“AddIdentity”方法自动添加了以下所有内容
// https://stackoverflow.com/questions/44483589/unable-to-resolve-service-for-type-microsoft-aspnetcore-identity-usermanager-w/48598575/#answer-56551234
AddHttpContextAccessor();
//身份识别服务
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
//没有用于错误描述符的接口,因此我们可以添加错误,而无需修改接口
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
// 
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}
我的控制器包含:

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.") }));
}
public异步任务登录([FromBody]UserModel)
{
var result=await _signingmgr.PasswordSignInAsync(model.Email,model.Password,false,false)。ConfigureAwait(true);
if(result.successed)
返回Ok();
其他的
返回状态码(StatusCodes.Status401Unauthorized,JsonConvert.SerializeObject(新的{error=ErrorHelper.SetControllerError(“无效用户名或密码”)});
}

执行
\u signInMgr.PasswordSignInAsync(model.Email,model.Password,false,false)
时发生错误,因为
.AddIdentityCore()
没有为您配置cookie。您需要调用
.AddIdentity()
来自动设置它。否则,你必须自己做

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

要查看名为
AddIdentity()
的服务集合扩展方法内部发生的情况,请查看与此代码相关的