本文共 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: BearerHTTP/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,如需转载请自行联系原作者