2017-05-11 9 views
2

Identity Server를 처음 사용했습니다. 나는 전에 그것을 구성하지 않았다. 하지만 내가 작업중인 프로젝트에 필요합니다.Identity Server 4 - invaid_client 오류 받기

API는 Angular JS Client, iOS App 및 Android App을 제공합니다. 우리는 인증과 인증을 구현해야합니다.

참고 : 동일한 웹 API 프로젝트에서 Identity Server와 내 API를 구성하려고합니다.

나는 다음과 같은 문서 및 구성 신원 서버 따랐다

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

: 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에서

을,

있음 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

I는 서버 측에서 얻을 오류 '찾을 수 없음 클라이언트 식별자'입니다 : enter image description here

1 - 왜이 오류가 무엇입니까를?

2 - JS, Android 및 iOS에서 프로그래밍 방식으로 Token을 얻으려면 /connect/token을 사용해야합니다.이 문제를 해결하려면 어떻게해야합니까? 나는 올바른 길을 가고 있는가?

+0

사용자를 어디에서 구성합니까? grant_type = password 및 username/password로 로그인하고 있습니다. 그것은 사용자가 존재하지 않는 것 같아요, –

+0

요청이 CustomResourceOwnerPasswordValidator에 도달하지 않습니다 - 그래서 신원 서버가 암호 유효성 검사기에 도달하기 전에 invalid_client 오류를 던지고 있음을 의미합니다. @RuardvanElburg –

답변

1

invalid_client 오류는 일반적으로 클라이언트 ID 또는 클라이언트 암호가 잘못되었음을 의미합니다. 이 경우 IdentityServer에 대한 요청에 클라이언트 비밀이 포함되지 않습니다. "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" } 
}; 

Heres는 코드 조각을 ClientSecret이 필요 없습니다 ClientSecretValidator.CS 정확한 오류는 JS, 안드로이드 및 iOS 용 토큰보기에 대한 두 번째 질문에 대해서는 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; 
} 

증거로 돌아가고, 당신은 오픈 ID 부여 유형 각에 사용할 것을 고려해야 할 수도 있습니다 대본. IdentityServer 개발자가 게시 한 일반적인 권장 사항은 웹 응용 프로그램 및 Authorization Code (또는 Hybrid) Flow에 대한 암시 적 플로우를 사용하는 것입니다. 여기에서 자세한 내용을 볼 수 있습니다. 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