在 .net 核心 openid 连接配置中重命名配置文件声明映射

Rename profile claim mapping in .net core openid connect configuration

我在配置文件范围内有多个声明被发回。这些声明包括:

员工类型 邮件 名字

这些 accessToken 声明正在自动映射到相同的名称。我希望将它们更改为如下更改映射:

员工类型 = 员工类型

邮件=邮件

givenName = 名字

我尝试使用 MapJsonKey() 但它不起作用我也尝试了 MapUniqueJsonKey()。我认为这些可能仅用于 userInfoClaims?

    options.ClaimActions.MapJsonKey("EmployeeType", "employeeType");
    options.ClaimActions.MapJsonKey("FirstName", "givenName");
    options.ClaimActions.MapJsonKey("Email", "Mail");

有没有办法将它们映射到不同的名称,或者我是否必须删除声明并使用 OnTokenValidated 挂钩将它们添加到 Prinical?

这是我在启动时的身份验证配置。

            services.AddAuthentication(options => {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
                {
                    o.Cookie.Name = "GCOWebCookie";
                    o.AccessDeniedPath = "/AccessDenied";
                })
                .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => {
                    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.Authority = config["OneId:Authority"];
                    options.ClientId = config["OneId:ClientId"];
                    options.ResponseType = "code";
                    options.ClientSecret = config["OneId:ClientSecret"];
                    options.SaveTokens = true;
                    //options.GetClaimsFromUserInfoEndpoint = true;
                    options.UsePkce = true;
                    //options.Scope.Add("profile"); These scopees are added by default
                    //options.Scope.Add("openid");

我会尝试改用这种方法:

options.ClaimActions.MapUniqueJsonKey("EmployeeType", "employeeType");

你在上面的问题中,只会映射 ID-token 的声明并将它们转换为 User 对象。 AddOpenIdConnect 不会对访问令牌的内容执行任何操作。它从不查看访问令牌内部。

然而,AddJwtBearer 仅侦听访问令牌,当您在 AddJwtBearer 内部进行映射时,访问令牌中的声明将映射到用户对象。您在接收访问令牌的后端 API 中使用的 AddJwtBearer。

要进一步自定义声明,您可以像 :

中显示的那样挂接到 OnTicketReceived 事件