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

2023-11-24

我在 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 => {});

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

发生异常:CLR/System.InvalidOperationException
抛出异常:System.Private.CoreLib.dll 中的“System.InvalidOperationException”:“没有为方案“Identity.Application”注册登录身份验证处理程序。注册的登录方案有: Cookie。您是否忘记调用 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 call

.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(通过以下方式添加到 DIAddAuthentication), see here
  • In AuthenticationService, it uses a chain of objects
    • IAuthenticationHandlerProvider => IAuthenticationSchemeProvider=> 之前配置的AuthenticationOptions构建一个AuthenticationScheme它提供了IAuthenticationHandler在这种情况下CookieAuthenticationHandler. see here and here and here
    • CookieAuthenticationHandler.HandleSignInAsync创建、加密并添加 cookie。

现在 cookie 已经存在,因此下一个请求(通常是登录后的重定向)AuthenticationMiddleware, the HttpContext.AuthenticateAsync方法被调用,它遵循类似的流程

  • CookieAuthenticationHandler.HandleAuthenticateAsync它读取 cookie 并传回ClaimsPrincipal,
  • 这被分配给HttpContext.User,使其可以访问请求管道的所有其他区域,例如授权,请参阅here
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.Net Core 2.2 SignInManager '没有为方案'Identity.Application'注册登录身份验证处理程序 的相关文章

  • 同一域上的多个 Django 站点 - CSRF 失败

    我有两个应用程序在同一域的不同端口上运行 都使用 csrf 中间件 当我登录其中一个应用程序时POST从另一个提交失败 我推测是因为SESSION COOKIE DOMAIN是一样的 我尝试改变SESSION COOKIE NAME 但是
  • 使用 Spring Security 时,SecurityContext 是否在请求之间共享?

    在使用 Spring Boot 编写的 Rest API 上使用基于无状态令牌的身份验证时 我看到一些奇怪的行为 客户端在每个请求中包含一个 JWT 令牌 并且我编写的扩展 GenericFilterBean 的自定义过滤器使用以下命令将基
  • 使用 ASP.Net 和 Ajax 的登录页面

    我正在尝试使用 html ajax 和 ASP NET 制作登录页面 数据确实传递给 ajax 函数 但是当我调试 asp 页面时 用户名和密码以 NULL 发送 该代码应该获取用户名和密码 然后返回用户 ID html页面 div Use
  • 如何使用我自己的自定义身份验证服务使用团队机器人对用户进行身份验证?

    我在 azure 上创建了一个机器人 并在机器人注册通道中注册了它 现在 我想使用我自己托管的自定义身份验证服务 例如 https domainname auth 来使用机器人对用户进行身份验证 我不知道该怎么做 我已阅读 bot 框架文档
  • Docker 多个相同端口问题

    我目前正在解决方案中开发两个相互关联的 ASP NET Core WebAPI 服务 Service1 和 Service2 两者都有 docker 文件并暴露端口 80 Service1是一个独立的服务 需要从Service2调用 我已经
  • websockets 如何处理同一浏览器的两个选项卡

    I have 1 个 PHP 服务器 提供 http 请求 和 1 node js 发布更新的数据消息 每个连接都带有 websocket php 服务器设置其 cookie 在一个浏览器中 此 cookie 可在所有选项卡中使用 当浏览器
  • 如何使用 ASP.NET Core 获取其他用户的声明

    我仍在学习 ASP NET Core 的身份 我正在进行基于声明的令牌授权 大多数示例都是关于 当前 登录用户的 就我而言 我的 RPC 服务正在接收身份数据库中某个用户的用户名和密码 我需要 验证是否存在具有此类凭据的用户 获取该用户的所
  • 如何在 Visual Studio 2017 中打开 dotnet-core CLI 项目

    我一直在使用 dotnet Core 用于 API 和 Angular2 开发一个简单的应用程序 以便学习两者 我在命令行上使用 Yeoman 创建了项目并且一直在 Visual Studio Code 在 Windows 上 中愉快地进行
  • 验证域用户凭据

    我需要一种方法来验证 Windows 上本机 C 的用户 密码对 输入的是用户名和密码 用户可以是 DOMAIN user 格式 基本上我需要编写一个函数 如果用户 密码是有效的本地帐户 则返回 true 第1部分 如果用户 密码在给定的域
  • OpenID 和 OAuth 之间有什么区别?

    我真的想了解 OpenID 和 OAuth 之间的区别 也许它们是两个完全不同的东西 OpenID http openid net 是关于身份验证 即证明您是谁 OAuth http oauth net 是关于授权的 即授予对功能 数据 等
  • 生成 Flask 中使用的签名会话 cookie 值

    我正在用另一个需要将项目注入会话的 Flask 服务器代理 Flask 服务器 两台服务器具有相同的密钥 因此加密签名将相同 当使用 Flask 和会话时 http 响应包含一个 Set Cookie 标头session text 其中 t
  • 错误:“运行所选代码生成器时出错:包恢复失败”

    我正在尝试将控制器添加到 ASP NET Core 项目中的解决方案中 当我尝试这样做时 我收到此错误 我收到相同的消息 为控制器添加最小依赖项和完整依赖项 我也有这个问题 使用实体框架添加控制器 gt 带有操作的 API 控制器 将给出
  • python - 将cookie添加到cookiejar

    如何在 python 中创建 cookie 并将其添加到 CookieJar 实例 我拥有 cookie 的所有信息 名称 值 域 路径等 但我不想通过 http 请求提取新的 cookie 我尝试了这个 但看起来 SimpleCookie
  • docker登录 - 存储凭据时出错 - 写入权限错误

    我正在运行一个docker login命令作为 Bamboo 构建作业的一部分 命令文本通过以下方式获得aws ecr get login调用并在子 shell 中执行 该命令失败并显示Error saving credentials er
  • Amazon S3:奇怪的错误 - “有时”签名不匹配,有时确实如此

    我正在为销售人员开发代码 我们正在使用 Appexchange 的 Force com for Amazon Web Services 应用程序 该应用程序由亚马逊提供 我正在计算机上从 Amazon S3 下载文件 有时我会收到下面提到的
  • 有关资源所有者密码授予类型的 ASP.NET Core 3 Identity/Identity Server/SPA 支持的问题

    As per SPA 的身份验证和授权 https learn microsoft com en us aspnet core security authentication identity api authorization view
  • Directory.GetCurrentDirectory() 不返回正确的目录[重复]

    这个问题在这里已经有答案了 在我的 ASP NET Core 2 2 MVC 服务器中 我想添加一个文件夹来存储静态文件 我找到了以下代码来执行该操作 app UseStaticFiles new StaticFileOptions Fil
  • .Net Core IdentityServer4 获取经过身份验证的用户

    我试图弄清楚如何使用 Net Core 2 从身份服务器 4 检索登录用户 我的身份验证当前正在工作 我只是想弄清楚如何从 HTTP 上下文中检索声明身份 services AddAuthentication options gt opti
  • App Engine 的 Google Cloud Storage 身份验证

    我们希望开始使用 Google Cloud Storage 作为用户上传文件的持久存储 不幸的是 我无法添加应用程序标识 应用程序 ID appspot gserviceaccount com 到我们的 团队 因为我们的团队基于 Googl
  • Chromecast CAF 接收器应用程序中的身份验证

    我有一个帮助 URL 在播放之前需要使用令牌进行身份验证 如何将令牌标头添加到接收方 CAF 应用程序 我在文档中进行了搜索 但找不到接收方 CAF 应用程序的任何身份验证参考 在 V2 播放器中我们可以拦截请求updateSegmentR

随机推荐