gpt4 book ai didi

authentication - ASP.Net Core 2.2 SignInManager 'No sign-in authentication handler is registered for the scheme ' Identity.Application'

转载 作者:行者123 更新时间:2023-12-03 14:48:40 28 4
gpt4 key购买 nike

我在 Startup.cs 中有以下配置:

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
//options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o => {
o.LoginPath = Routes.Urls.AdminAccountLogin;
o.AccessDeniedPath = Routes.Urls.AdminAccountAccessdenied;
}).AddJwtBearer(configureOptions => {});

当 Controller 登录操作调用 SignInManger.PasswordSignInAsync 时,应用程序抛出以下异常:

Exception has occurred: CLR/System.InvalidOperationException
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll: 'No sign-in authentication handler is registered for the scheme 'Identity.Application'. The registered sign-in schemes are: Cookies. Did you forget to call AddAuthentication().AddCookies("Identity.Application",...)?'


Identity.Application 在哪里来自?

最佳答案

简短(并且没有帮助)答案:

具体来说,它来自 microsoft.aspnetcore.identity类中的包Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme
长答案,整个分割:

您需要添加身份 - 该方案已建立并连接到 AddIdentity 中的身份验证扩展方法

扩展方法在Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionExtensions

public static IdentityBuilder AddIdentity<TUser, TRole>(this IServiceCollection services, Action<IdentityOptions> setupAction) where TUser: class where TRole: class
{
services.AddAuthentication(delegate (AuthenticationOptions options) {
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
}).AddCookie(IdentityConstants.ApplicationScheme, delegate (CookieAuthenticationOptions o) {
o.LoginPath = new PathString("/Account/Login");
CookieAuthenticationEvents events1 = new CookieAuthenticationEvents();
events1.OnValidatePrincipal = new Func<CookieValidatePrincipalContext, Task>(SecurityStampValidator.ValidatePrincipalAsync);
o.Events = events1;
}).AddCookie(IdentityConstants.ExternalScheme, delegate (CookieAuthenticationOptions o) {
o.Cookie.Name = IdentityConstants.ExternalScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes((double) 5.0);
}).AddCookie(IdentityConstants.TwoFactorRememberMeScheme, delegate (CookieAuthenticationOptions o) {
o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme;
CookieAuthenticationEvents events1 = new CookieAuthenticationEvents();
events1.OnValidatePrincipal = new Func<CookieValidatePrincipalContext, Task>(SecurityStampValidator.ValidateAsync<ITwoFactorSecurityStampValidator>);
o.Events = events1;
}).AddCookie(IdentityConstants.TwoFactorUserIdScheme, delegate (CookieAuthenticationOptions o) {
o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes((double) 5.0);
});
services.AddHttpContextAccessor();
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
services.TryAddScoped<UserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>>();
if (setupAction != null)
{
services.Configure<IdentityOptions>(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}

如果您关注此 AddCookie称呼

.AddCookie(IdentityConstants.ApplicationScheme, delegate (CookieAuthenticationOptions o) {
o.LoginPath = new PathString("/Account/Login");
CookieAuthenticationEvents events1 = new CookieAuthenticationEvents();
events1.OnValidatePrincipal = new Func<CookieValidatePrincipalContext, Task>(SecurityStampValidator.ValidatePrincipalAsync);
o.Events = events1;

它最终配置 AuthenticationOptions使用“Identity.Application”方案和 CookieAuthenticationHandler
当您调用 SignInManager.PasswordSignInAsync :
  • SignInManager检查数据库中的用户名/密码(如果启用,则执行两个因素流),然后如果正确
  • 创建 ClaimsPrincipal并将其发送至 HttpContext.SignInAsync (一种扩展方法)与身份应用方案,见here
  • 得到 IAuthenticationService (由 AddAuthentication 添加到 DI),见 here
  • AuthenticationService ,它使用一个对象链
  • IAuthenticationHandlerProvider => IAuthenticationSchemeProvider => 之前配置的AuthenticationOptions构建 AuthenticationScheme提供IAuthenticationHandler在这种情况下 CookieAuthenticationHandler .见 hereherehere
  • CookieAuthenticationHandler.HandleSignInAsync创建、加密和添加 cookie。

  • 现在 cookie 就在那里,所以 AuthenticationMiddleware 中的下一个请求(通常是登录后的重定向) , HttpContext.AuthenticateAsync方法被调用,它遵循类似的流程
  • CookieAuthenticationHandler.HandleAuthenticateAsync它读取 cookie 并传回 ClaimsPrincipal ,
  • 这已分配给 HttpContext.User ,使其可访问请求管道的所有其他区域,如授权,请参阅 here
  • 关于authentication - ASP.Net Core 2.2 SignInManager 'No sign-in authentication handler is registered for the scheme ' Identity.Application',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521125/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com