asp.net-core - IdentityServer4 无法读取所有声明
问题描述
我在 asp.net core 2.2 中使用 IdentityServer4。
我的获取个人资料数据是:
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var sub = context.Subject.GetSubjectId();
var user = await _userManager.FindByIdAsync(sub);
var principal = await _claimsFactory.CreateAsync(user);
var claims = principal.Claims.ToList();
claims.Add(new Claim(ClaimTypes.NameIdentifier, "fullname"));
context.IssuedClaims = claims;
}
我添加了客户端如下:
new Client
{
ClientId = "client",
ClientName = "Application",
AllowedGrantTypes = GrantTypes.Hybrid,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:45876/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:45876/signout-callback-oidc" },
AllowedCorsOrigins = new[] { "http://localhost:45876/" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"webapi"
},
AllowOfflineAccess = true
},
当我在客户中阅读声明时,我有一些声明并非全部主题。名称标识符为空?
httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
我的问题是什么?我应该怎么办?
解决方案
Profile Service 将帮助动态添加用户的声明。该声明可以添加到身份令牌、访问令牌或用户信息端点中。常量IdentityServerConstants.ProfileDataCallers
包含不同的常量值。您可以通过过滤来跟踪逻辑context.Caller
。
关注 ID Token 的长度,默认情况下,声明不会包含在 ID token 中。您可以使用以下解决方案之一将自定义声明添加到客户端应用的用户声明原则:
在客户端应用程序中,您可以
options.GetClaimsFromUserInfoEndpoint = true
在 OpenID 连接选项中进行设置,以便客户端应用程序向 OIDC 的 userinfo 端点发送请求以获取额外的声明,然后您可以使用ClaimActions.MapJsonKey
将声明映射到用户声明/cookie:options.GetClaimsFromUserInfoEndpoint = true; options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, ClaimTypes.NameIdentifier);
另一种解决方案是在 Identity Server 中注册客户端时设置
AlwaysIncludeUserClaimsInIdToken
自true
定义声明将添加到 id 令牌,客户端应用程序将直接将声明从 ID 令牌映射到用户声明原则:AlwaysIncludeUserClaimsInIdToken =true
推荐阅读
- node.js - 尝试在 node.js 中使用 axios 复制 curl -c post/get
- unity3d - 一个一个地显示 10 个单独的网格,并具有循环行为以为空的游戏对象创建“动画”?
- javascript - 当页面在 IE11 中冻结时,如何防止触发 OnBlur 事件
- python - 如何在python中的csv文件的起始位置添加列?
- python - Django模型视图集内置删除方法不起作用
- objective-c - WKWebView:如何在 objc 中打开另一个 WKWebView
- python - pyparsing 以与输入文本相同的顺序对匹配的字符串和不匹配的字符串进行分组
- javascript - 将两个节点应用服务器连接到单个 mongo db 数据库
- r - r:在数据框中将过滤器与 n_distinct 组合
- google-data-studio - 如何从百分比或货币中删除小数