IdentityServer4 通过 AccessToken 获取 UserClaims

简介:

实现效果:通过生成的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;

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

相关文章
|
27天前
|
JSON JavaScript 数据格式
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能。
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能。
43 1
|
23天前
|
JSON JavaScript 数据格式
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能
19 1
|
10月前
|
存储 NoSQL 数据库
Oauth2协议中如何对accessToken进行校验
Oauth2协议中如何对accessToken进行校验
125 0
|
8月前
|
存储 JSON 安全
探索JSON Web Token(JWT):现代身份验证和授权的利器
在现代Web应用中,用户身份验证和授权是保护数据和资源安全的重要环节。JSON Web Token(JWT)作为一种轻量级的身份验证和授权机制,为我们提供了一种安全且高效的方式来传递信息。本文将深入探讨JWT的基本概念、结构,以及如何在应用中实现安全的身份验证和授权。
120 0
|
存储 JSON 算法
用户鉴权、JWT(JSON Web Token)是什么?
用户鉴权、JWT(JSON Web Token)是什么?
373 0
用户鉴权、JWT(JSON Web Token)是什么?
|
安全 JavaScript 前端开发
13-SpringSecurity:OpenID与Keycloak
13-SpringSecurity:OpenID与Keycloak
626 0
13-SpringSecurity:OpenID与Keycloak
|
存储 JSON 安全
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(上)
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(上)
577 0
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(上)
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(下)
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(下)
374 0
FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证(下)
|
数据安全/隐私保护
到底什么是 OAuth 2.0?
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为RFC 6749。
到底什么是 OAuth 2.0?