0

리소스 소유자 비밀번호 흐름을 사용할 때 IdentityServer4와 함께 fetch를 사용하여 JWT 토큰을 얻는 방법은 무엇입니까? (대부분 권장되지는 않음).Identity Server 4와 함께 페치 사용

암시 적 플로우를 사용해야하는 경우에도 내 스파 적용을 위해 제 상황에서보다 편리하기 때문에이 플로우를 사용하고 싶습니다. 먼저 , 당신은 당신의 솔루션을 구현하기 위해 여기에 유용한 정보를 찾을 수 있습니다 :

을하지만 사용하는 방법 [가져-API를]에서 토큰을 얻기 위해 IdentityServer 4?

const form = new FormData(); 
    form.append('grant_type', 'password'); 
    form.append('username', username); 
    form.append('password', password); 
    if (this._settings.scope) { 
     form.append('scope', this._settings.scope); 
    } 
    form.append('client_id', this._settings.clientId); 
    if (this._settings.clientSecret) { 
     form.append('client_secret', this._settings.clientSecret); 
    } 

    var options = { 
     method: 'POST', 
     headers: { 
      'Content-Type': 'multipart/form-data' 
     }, 
     rejectUnauthorized: false, // when use local unverified certificate 
     body: form 
    }; 

응답으로 JWT 토큰을 받아야하지만 http 500 오류가 발생합니다. "내부 서버 오류"

내가 피들러와 HTTP 헤더를 추적 경우에, 나는 다음과 같은 결과를 얻었다 :

POST http://127.0.0.1:8888/ HTTP/1.1 
content-type: multipart/form-data 
accept-encoding: gzip,deflate 
user-agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch) 
connection: close 
accept: */* 
content-length: 896 
Host: 127.0.0.1:8888 
+1

identityserver 로그의 내용은 무엇입니까? – cheesemacfly

+0

'dbug : IdentityServer4.Validation.PostBodySecretParser [0] 후 몸 치명타 비밀에 대한 분석을 시작 : IdentityServer4.Hosting.IdentityServerMiddleware를 [0] 처리되지 않은 예외 : System.IO.InvalidDataException : ** 내용 유형 경계가 없습니다. ** Microsoft.AspNetCore.Http.Features.FormFeature.GetBoundary (MediaTypeHeaderValue의 contentType, INT32 lengthLimit) Microsoft.AspNetCore.Http.Features.FormFeature에서 에서. d__18.MoveNext() --- 예외가 발생한 이전 위치에서 스택 추적 끝 --- – Armand

+0

"헤더"에서 "content-type"을 제거하면 작동합니다! 나중에 답을 줄뿐만 아니라이 코드는 요청 js 모듈과 함께 작동하는 복사/붙여 넣기에서 나온 것입니다. (전달 된 content-type을 덮어 쓰기 요청) – Armand

답변

0

내 이전 코멘트에서 언급 한 바와 같이 : 당신은 공인 POST하기 위해 Fetch를 사용하려면 양식 데이터로 요청할 경우 헤더에 content-type을 지정하지 않아야합니다. fetch은 올바른 값 ("multipart/form-data")을 자동으로 채우고 동적으로 생성 된 콘텐츠 경계를 추가합니다. application/x-www-form-urlencoded 콘텐츠 유형 방법을 사용하려는 경우 URLSearchParams을 사용할 수 있습니다.