You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenIddict Hollastin示例时Password Flow无法返回Principal声明问题

解决OpenIddict客户端Principal缺少Email/Name声明的问题

原因分析

默认情况下,OpenIddict客户端不会自动将令牌中的所有声明映射到ClaimsPrincipal,需手动配置声明映射规则,或确保客户端请求令牌时指定了所需的声明范围。

解决方案

1. 请求令牌时指定所需范围

调用AuthenticateWithPasswordAsync时,明确包含email、profile等需要的范围:

var result = await _openIddictClientService.AuthenticateWithPasswordAsync(new()
{
    Username = "你的用户名",
    Password = "你的密码",
    Scope = { "openid", "email", "profile" }
});

2. 配置客户端声明映射

在客户端项目的Program.cs中,添加声明映射规则,将令牌中的字段映射到ClaimsPrincipal:

builder.Services.AddOpenIddict()
    .AddClient(options =>
    {
        // 保留原有客户端配置...

        options.AddClaims(options =>
        {
            options.MapClaim(ClaimTypes.Email, "email");
            options.MapClaim(ClaimTypes.Name, "name");
            // 按需添加其他需要映射的声明
        });
    });

3. 检查服务器端配置

确认OpenIddict服务器端已正确配置:

  • 客户端的AllowedScopes包含email、profile
  • 服务器已注册email、profile这些范围
  • 用户实体正确返回Email、Name对应的声明数据

内容的提问来源于stack exchange,提问作者valentasm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 09:23:12