[英]Not getting user “email” as a claim (from jwt token) back from identity server
我在IdentityServer上有一个客户端,它允许openid,profile和email作用域:
return new[] {
new Client
{
ClientId = "TestWebApp",
ClientSecrets = new [] { new Secret("TestSecret".Sha256()) },
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
AllowedScopes = new List<string>{ StandardScopes.OpenId, StandardScopes.Profile,StandardScopes.Email },
}
};
我也定义了以下身份资源,
public static IEnumerable<IdentityResource> IdentityResources()
{
return new IdentityResource[] {
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
}
万一声明丢失,我将在创建时向用户声明中明确添加电子邮件:
await _userManager.AddClaimAsync(testUser, new Claim("email", user.Username));
现在从我的登录控制器使用ResourceOwnerPasswordAndClientCredentials
发送身份验证请求:
var client = new OAuth2Client(new Uri("http://localhost:44322/connect/token"), "TestWebApp", "TestSecret");
var requestResponse = client.RequestAccessTokenUserName(model.Email, model.Password, "openid profile email");
这可以很好地工作,并且我可以将作用域恢复原状,但是所有作用域都为空白。
在范围上指定用户声明时,可以在accesstoken中包括这些声明。 例如,对于Swagger,我们需要包括名称声明(如果可用),下面我转储了ApiResource类应包含的内容。
{
"ApiSecrets": [],
"Scopes": [
{
"Name": "SwaggerApi",
"DisplayName": "SwaggerApi",
"Description": null,
"Required": true,
"Emphasize": false,
"ShowInDiscoveryDocument": true,
"UserClaims": ["name","email"]
}
],
"Enabled": true,
"Name": "SwaggerApi",
"DisplayName": "SwaggerApi",
"Description": null,
"UserClaims": ["name","email"]
}
将此范围添加到您的客户注册的允许范围。
请求访问令牌。
如果用户有名称声明或电子邮件声明->应该将其添加到访问令牌中。
结果内容访问令牌
"idp": "oidc",
"name": "MyUserName",
"scope": [
"openid",
"profile",
"SwaggerApi"
],
使用资源所有者密码流时,您是在请求访问令牌,而不是ID令牌。 因此,在创建访问令牌时,与定义为身份资源的范围相关联的声明不会传递到您的注册配置文件服务实现中。 如果您确实要在访问令牌中包含电子邮件,则建议您使用定义为声明类型的“电子邮件”来创建api资源范围。
话虽如此,如果电子邮件用于身份验证目的,我建议您使用另一个登录流,如果可能的话,该登录流允许身份令牌,或者使用用户信息端点。
如果要在ID令牌中包括用户声明,则可以在客户端配置上将AlwaysIncludeUserClaimsInIdToken设置为true。
return new[] {
new Client
{
ClientId = "TestWebApp",
ClientSecrets = new [] { new Secret("TestSecret".Sha256()) },
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
AllowedScopes = new List<string>{ StandardScopes.OpenId,
StandardScopes.Profile,StandardScopes.Email },
AlwaysIncludeUserClaimsInIdToken = true
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.