[英]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.