IdentityServer4 通过 AccessToken 获取 UserClaims
2023-02-18 15:32:39 时间
实现效果:通过生成的access_token
获取用户的一些信息,这样客户端请求的时候,不需要传递用户信息了。
示例配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryIdentityResources(new List<IdentityResource>
{
new IdentityResources.OpenId(), //必须要添加,否则报无效的scope错误
new IdentityResources.Profile(),
})
.AddInMemoryApiResources(new List<ApiResource>
{
new ApiResource("api1", "My API")
})
.AddInMemoryClients(new List<Client>
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes =
{
"api1",
IdentityServerConstants.StandardScopes.OpenId, //必须要添加,否则报forbidden错误
IdentityServerConstants.StandardScopes.Profile
}
}
});
}
Http 调用示例:
GET /connect/userinfo
Authorization: Bearer <access_token>
HTTP/1.1 200 OK
Content-Type: application/json
{
"sub": "248289761001",
"name": "Bob Smith",
"given_name": "Bob",
"family_name": "Smith",
"role": [
"user",
"admin"
]
}
UserInfoClient
调用示例:
var token = "";
var client = new DiscoveryClient(_appSettings.IssuerUri);
client.Policy.RequireHttps = false;
var disco = await client.GetAsync();
var userInfoClient = new UserInfoClient(doc.UserInfoEndpoint);
var response = await userInfoClient.GetAsync(token);
var claims = response.Claims;
参考资料:
相关文章
- 【Docker】搭建部署Redis高可用集群实验
- 【K8S】kubernetes概念和架构(一)
- 【K8S】基于Windows系统下使用minikube安装kubernetes集群
- PromQL的简单使用
- Prometheus基于文件的服务发现
- Prometheus重新标记
- Prometheus的单机部署
- Vim的简单使用
- SpringBoot整合kafka
- kafka生产者和消费者api的简单使用
- kafka错误之 Topic xxx not present in metadata after 60000 ms
- kafka的基本概念
- 处理微信回调事件
- 微信公众号开发接入
- kafka集群搭建
- Spring Cloud Alibaba Nacos Config 的使用
- Spring Cloud Gateway GatewayFilter的使用
- Spring Cloud Gateway Route Predicate Factory 的使用
- Charles的简单用法
- Spring Cloud Alibaba整合Sentinel