2017-02-22 13 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-requestが必要なのは何ですか?

さらに、私のクライアントにapiを設定する方法は、の特定のユーザーのトークンにアクセスすることができますか?

答えて

1

上記の設定を見ると、設定したクライアントはClientCredentials許可を使用しています。これはOAuth許可タイプです。 IDを追加する場合は、OpenID Connect認可タイプを使用する必要があります。これにより、認証するユーザに固有のIDとトークンが提供されます。アイデンティティ・サーバーでのOpenID Connectを使用して上の

詳しい情報は、以下のクイックスタートチュートリアルを作業する場合、あなたはそれが一緒に表示されますhttp://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html

0

でドキュメントに記載されています。

  • セットアップと概要
  • のOpenID Connectにハイブリッドフローへ
  • 切り替えをユーザー認証を追加し、バック
をAPIへのアクセスを追加するパスワード
  • を使用してAPIを保護するクライアントの資格情報
  • を使用してAPIの保護

    (外部認証の場合はスキップしてください)

    https://identityserver4.readthedocs.io/en/release/quickstarts/0_overview.html

  • 関連する問題