2017-05-11 7 views
2

Identity Serverを初めて使用しています。私はこれまでに設定していません。しかし、私が取り組んでいるプロジェクトにはそれが必要です。Identity Server 4 - invaid_clientエラーの取得

APIは、Angular JS Client、iOS App、Android Appを提供します。認証と承認を実装する必要があります。

注:同じWeb APIプロジェクトでIdentity ServerとそのAPIを設定しようとしています。

私はマニュアルを参照して、その後、次のようIdentity Serverのを設定している:ConfigureServices()

 services.AddTransient<IProfileService, CustomProfileService>(); 
     services.AddTransient<IResourceOwnerPasswordValidator, CustomResourceOwnerPasswordValidator>(); 



     services.AddIdentityServer() 
      .AddTemporarySigningCredential() 
      // add the resources that need to be secured 
      .AddInMemoryApiResources(IdentityServerConfig.Resources.GetApiResources()) 
      // add the clients that will be access the ApiResources 
      .AddInMemoryClients(IdentityServerConfig.Clients.GetClients()); 

CustomProfileServiceCustomResourceOwnerPasswordValidatorとこの答えと同じでstartup.csで

、:https://stackoverflow.com/a/35306021/1910735

n内Configure()

ここで

10はGetClients()

public static IEnumerable<Client> GetClients() 
    { 
     var clients = new List<Client>(); 



     var websiteGrants = new List<string> { GrantType.ResourceOwnerPassword }; 
     var secret = new Secret("secret".Sha256()); 

     var websiteClient = new Client() 
     { 
      // we will be using Angular JS to access the API - so naming it js 
      ClientId = "js", 

      // just a human friendly name 
      ClientName = "JavaScript Client", 

      // set to GrantType.ResourceOwnerPassword - because using Username/Password to login 
      AllowedGrantTypes = websiteGrants, 

      // secret for authentication 
      //TODO: Change the secret 
      ClientSecrets = { secret }, 

      // we need to access the fhApi from Angular JS front-end 
      // fhApi is defined in Resources file as an API Resource 
      AllowedScopes = { "obApi" } 
     }; 


     clients.Add(websiteClient); 

     return clients; 
    } 

であり、ここで私が使用したいので、今GetApiResources()

public static IEnumerable<ApiResource> GetApiResources() 
    { 
     // e.g. if we want to protect an API called api1 - then we will add it here 
     // these values are hard coded for now - but we can get from DB, config file etc. 

     return new List<ApiResource> 
         { 
          new ApiResource("obApi", "Order2Bite API") 
         }; 
    } 

である私はちょうどからのアクセストークンを取得したいことアンギュラJS、iOSとAndroidのIdentity Serverを開き、認証トークンと認証トークンを使用します。このため

私はJSクライアント

から/connect/tokenにアクセスしようとしています。しかし、私はinvalid_clientエラーを取得しています。

 var user = { client_id: "js", grant_type: 'password', username: "testuser", password: "testpasswrd", scope: 'obApi' }; 

     var urlEncodedUrl = { 
      'Content-Type': 'application/x-www-form-urlencoded', 
     }; 

     this.$http({ 
      method: 'POST', url: "http://localhost:44337/connect/token", 
      headers: urlEncodedUrl, 
      data: user, 

     }) 
      .then(data => { 
       console.log(data) 
      }, 
      data => { 
       console.log(data) 

      }); 

enter image description here

私は、サーバー側で取得エラー 'が見つかりませクライアント識別子' ではありません: enter image description here

1 - なぜ私はこのエラーを取得していますか?

2 - JS、Android、およびiOSでトークンをプログラムで取得する必要があるため、/connect/tokenを使用する必要があります。これは正しいですか?私は正しい道にいるのですか?

+0

ここで、ユーザーを設定していますか?あなたはgrant_type = passwordとusername/passwordでログインしています。ユーザーが存在しないようですが、 –

+0

要求がCustomResourceOwnerPasswordValidatorに到達していません。つまり、IDサーバーがパスワードバリデーターに到達する前にIDサーバーがinvalid_clientエラーをスローしていることを意味します。@RuardvanElburg –

答えて

1

invalid_clientエラーは、通常、クライアントIDまたはクライアントシークレットが正しくないことを意味します。この場合、IdentityServerへのリクエストにClient Secretは含まれません。あなたの要求に

更新データ: " '秘密' client_secretを":追加または

var user = { client_id: "js", client_secret: "secret", grant_type: 'password', username: "testuser", password: "testpasswrd", scope: 'obApi' }; 

、あなたが相続人クライアントの設定でIdentityServer4からスニペットを

var websiteClient = new Client() 
{ 
    // we will be using Angular JS to access the API - so naming it js 
    ClientId = "js", 

    // just a human friendly name 
    ClientName = "JavaScript Client", 

    // set to GrantType.ResourceOwnerPassword - because using Username/Password to login 
    AllowedGrantTypes = websiteGrants, 

    // secret for authentication 
    //TODO: Change the secret 
    ClientSecrets = { secret }, 

    // Disable client secret validation 
    RequireClientSecret = false, 

    // we need to access the fhApi from Angular JS front-end 
    // fhApi is defined in Resources file as an API Resource 
    AllowedScopes = { "obApi" } 
}; 

をClientSecretを要求することはできませんClientSecretValidator。あなたはJS、アンドロイド、およびiOS用のトークンを得ることについて、あなたの2番目の質問については、バック証明 https://github.com/IdentityServer/IdentityServer4/blob/release/src/IdentityServer4/Validation/ClientSecretValidator.cs

var parsedSecret = await _parser.ParseAsync(context); 
if (parsedSecret == null) 
{ 
    await RaiseFailureEvent("unknown", "No client id found"); 

    _logger.LogError("No client identifier found"); 
    return fail; 
} 

として取得している正確なエラーとCS、あなたがそれぞれに使用するどのOpenIDのグラントの種類を検討する必要がある場合がありますシナリオ。 IdentityServerの開発者から投稿された一般的な推奨事項は、WebアプリケーションとAuthorization Code(またはHybrid)フローの暗黙のフローを使用することです。詳細については、こちらをご覧ください。 http://docs.identityserver.io/en/release/topics/grant_types.html

+0

はい、client_secretを含めると機能しますが、ドキュメントでは、client_secretはOPTIONALです。 http://docs.identityserver.io/en/release/endpoints/token.html –

+0

最新の回答をご覧ください。 Clientのデフォルトのコンストラクタは、RequireClientSecretをtrueにデフォルト設定します。クライアントの秘密を要求したくない場合は、falseに設定するだけです。 正しいですが、ResourceOwnerフロー認証にクライアントシークレットを含める必要はありません。 IdentityServer3はそれを要求しましたが、v4はもはやIIRCを使用しません。 – kg743

関連する問題