【发布时间】:2019-07-28 22:44:48
【问题描述】:
我在 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");
这很好,我正在拿回范围,但它们都是空白的。
【问题讨论】:
-
您可能会获得访问令牌,它使您访问到指定的范围。但要获取电子邮件和其他用户信息,您应该获取 id_token。
-
或者您可以调用 UserInfo 端点提供此访问令牌。
-
@SlavaUtesinov,你有文章的例子或参考吗?
标签: c# asp.net-core asp.net-identity identityserver4