Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在identityserver4中使用访问令牌?_C#_Asp.net Core_Identityserver4 - Fatal编程技术网

C# 如何在identityserver4中使用访问令牌?

C# 如何在identityserver4中使用访问令牌?,c#,asp.net-core,identityserver4,C#,Asp.net Core,Identityserver4,该场景是我的客户获取我的用户数据的可能性。配置服务器的代码如下: Startup.cs services.AddIdentityServer() .AddTemporarySigningCredential() .AddInMemoryIdentityResources(ApiConfiguration.GetIdentityResources()) .AddInMemoryApiResources(ApiConfiguration

该场景是我的客户获取我的用户数据的可能性。配置服务器的代码如下:

Startup.cs

services.AddIdentityServer()
         .AddTemporarySigningCredential()
         .AddInMemoryIdentityResources(ApiConfiguration.GetIdentityResources())               .AddInMemoryApiResources(ApiConfiguration.GetAllResources())
         .AddInMemoryClients(ApiConfiguration.GetAllClients())
         .AddTestUsers(ApiConfiguration.GetUsers())
顶点配置

public static IEnumerable<ApiResource> GetAllResources()
        {
            yield return new ApiResource
            {
                Name = "customAPI",
                Description = "Custom API Access",
                UserClaims = new List<string> { "role" },
                ApiSecrets = new List<Secret> { new Secret("scopeSecret".Sha256()) },
                Scopes = new List<Scope> {
                    new Scope("customAPI"),
                }
            };
        }
public static IEnumerable<Client> GetAllClients()
        {
            yield return new Client
            {
                ClientId = "oauthClient",
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                ClientSecrets = new List<Secret> {
                    new Secret("Password".Sha256())},
                AllowedScopes = new List<string> { "customAPI" }
            };
        }
public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
    {
new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResources.Email(),
    };
        }

public  static List<TestUser> GetUsers()
        {
            return new List<TestUser>
    {
        new TestUser
        {
            SubjectId = "2",
            Username = "bob",
            Password = "psw",
             Claims = new List<Claim> {
                    new Claim(ClaimTypes.Email, "1@1.com"),
                    new Claim(ClaimTypes.Role, "admin")
                }
        }
    };
        }
客户端获得了访问令牌。因此,我的问题是如何使用令牌?我需要什么http请求来获取关于bob(测试用户)的数据


还有另一个相关的问题:如何将api配置到我的客户端?我的客户端只能访问特定用户的令牌?

查看上面的配置,您设置的客户端正在使用ClientCredentials授权,这是OAuth授权类型。如果您希望添加标识,您应该使用OpenID连接授权类型,这将为您提供特定于您认证为的用户的标识和令牌


有关将OpenID Connect与Identity Server结合使用的更多信息,请参阅位于

的文档。如果您阅读了以下快速入门教程,您将看到它们的结合

  • 设置和概述
  • 使用客户端凭据保护API
  • 使用密码保护API
  • 使用OpenID Connect添加用户身份验证
  • 切换到混合流并重新添加API访问
(您可以跳过关于外部身份验证的设置。)

POST /connect/token
Headers:
Content-Type: application/x-www-form-urlencoded
Body:
grant_type=client_credentials&scope=customAPI&client_id=oauthClient&client_secret=Password