博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IdentityServer4 通过 AccessToken 获取 UserClaims
阅读量:7032 次
发布时间:2019-06-28

本文共 1783 字,大约阅读时间需要 5 分钟。

实现效果:通过生成的access_token获取用户的一些信息,这样客户端请求的时候,不需要传递用户信息了。

示例配置:

public void ConfigureServices(IServiceCollection services){    services.AddIdentityServer()        .AddTemporarySigningCredential()        .AddInMemoryIdentityResources(new List
{ new IdentityResources.OpenId(), //必须要添加,否则报无效的scope错误 new IdentityResources.Profile(), }) .AddInMemoryApiResources(new List
{ new ApiResource("api1", "My API") }) .AddInMemoryClients(new List
{ new Client { ClientId = "client", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets = { new Secret("secret".Sha256()) }, AllowedScopes = { "api1", IdentityServerConstants.StandardScopes.OpenId, //必须要添加,否则报forbidden错误 IdentityServerConstants.StandardScopes.Profile } } });}

Http 调用示例:

GET /connect/userinfoAuthorization: Bearer 
HTTP/1.1 200 OKContent-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;

本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/identityserver4-get-user-claims-by-token.html,如需转载请自行联系原作者

你可能感兴趣的文章
BZOJ 1084 最大子矩阵
查看>>
2018杭电多校第三场1007(凸包,极角排序)
查看>>
django中orm的简单操作
查看>>
Mybatis知识(1)
查看>>
[CentOS] 7 不执行文件 /etc/rc.d/rc.local
查看>>
模态窗口的各个属性
查看>>
10.28 (上午) 开课一个月零二十四天 (数据访问)
查看>>
为什么你应该(从现在开始就)写博客
查看>>
小技巧积累
查看>>
Java JDBC链接Oracle数据库
查看>>
Moss2010 部署命令
查看>>
Git 操作分支
查看>>
Grid search in the tidyverse
查看>>
hdu 三部曲 Contestants Division
查看>>
day22——创建表、增加数据、查询数据
查看>>
css伪元素实现tootip提示框
查看>>
关于函数指针的总结
查看>>
采用PHP函数uniqid生成一个唯一的ID
查看>>
Centos7安装32位库用来安装32位软件程序
查看>>
【HMOI】小C的填数游戏 DP+线段树维护
查看>>