identityserver4 - IdentityServer4 - RequestedClaimTypes 为空
问题描述
来自 IdentityServer 4 文档: 如果请求的范围是身份资源,则 RequestedClaimTypes 中的声明将根据 IdentityResource 中定义的用户声明类型填充
这是我的身份资源:
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Phone(),
new IdentityResources.Email(),
new IdentityResource(ScopeConstants.Roles, new List<string> { JwtClaimTypes.Role })
};
这是我的客户
AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Phone,
IdentityServerConstants.StandardScopes.Email,
ScopeConstants.Roles
},
ProfileService - GetProfileDataAsync 方法:
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 = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();
if (user.Configuration != null)
claims.Add(new Claim(PropertyConstants.Configuration, user.Configuration));
context.IssuedClaims = claims;
}
principal.claims.ToList() 列出了所有声明,但 context.RequestedClaimTypes 为空,因此 context.RequestedClaimTypes.Contains(claim.Type)) 的过滤器不返回任何声明。
客户端配置:
让 header = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
let params = new HttpParams()
.append('username', userName)
.append('password', password)
.append('grant_type', 'password')
.append('scope', 'email offline_access openid phone profile roles api_resource')
.append('resource', window.location.origin)
.append('client_id', 'test_spa');
let requestBody = params.toString();
return this.http.post<T>(this.loginUrl, requestBody, { headers: header });
响应类型:
export interface LoginResponse {
access_token: string;
token_type: string;
refresh_token: string;
expires_in: number;
}
有人表示添加 AlwaysIncludeUserClaimsInIdToken = true 可以解决问题 - 我试过了,但没有。
我在这里想念什么?请帮忙。
解决方案
推荐阅读
- python - 在 Python 中模拟 Azure Functions httpRequest 表单
- jupyter-notebook - 笔记本电脑重建时 jupyter 笔记本丢失
- excel - 有没有办法根据另一个单元格上的不同值突出显示单元格
- python - 如何使用 SQLAlchemy 的 to_sql 插入 VARBINARY 列?
- reactjs - reactjs:提供来自不同服务器的资产
- ios - Swift Package 平台是最低版本吗?
- python - 如何在 Python 中创建构建文件或 sh 文件
- android - 如何检查在 Wear OS 3(Samsung Galaxy Watch 4)上运行的应用程序上可用的互联网
- mysql - 如何在 MySQL 中查找和插入丢失的数据而不创建重复数据
- kubernetes - 是否可以在 Mutating Webhook 中修改对象命名空间?