2017-02-22 21 views
0

这种情况是我的客户可以获取有关我的用户的数据。没有为配置服务器的代码:如何在identityserver4中使用访问令牌?

Startup.cs

services.AddIdentityServer() 
     .AddTemporarySigningCredential() 
     .AddInMemoryIdentityResources(ApiConfiguration.GetIdentityResources())    .AddInMemoryApiResources(ApiConfiguration.GetAllResources()) 
     .AddInMemoryClients(ApiConfiguration.GetAllClients()) 
     .AddTestUsers(ApiConfiguration.GetUsers()) 

ApiConfiguration

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, "[email protected]"), 
        new Claim(ClaimTypes.Role, "admin") 
       } 
     } 
    }; 
     } 

有了这个请求:

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

客户拿到了访问令牌。所以我的问题是我该如何使用令牌?我需要http请求来获取有关bob(测试用户)的数据?

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

回答

1

查看上面的配置,您设置的客户端正在使用ClientCredentials授予,这是一种OAuth授予类型。如果您希望添加身份,您应该使用OpenID Connect授权类型,这将为您提供特定于您身份验证的用户的身份和令牌。

使用ID连接与Identity Server的更多信息可以在文档在http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html

相关问题