2017-02-07 15 views
0

.Net Framework 4.5에서 작동하지만 내가. NET 3.5에서 동등한 기능을 필요로하는 코드가 있습니다. 그리고 제 문제는 거의 모든 Google 검색 결과가 새로운 WIF를 사용하는 솔루션 또는 이전 WIF 3.5에 대한 일반 정보를 얻게된다는 것입니다..NET 3.5에서 AuthenticationContext.AcquireToken 해당 항목

코드는 다음과 같습니다

using Microsoft.IdentityModel.Clients.ActiveDirectory; 

namespace x 
{ 
    class y 
    { 
     public string GetAuthenticationHeader(Ax7Config config) 
     { 
      var user = new UserCredential(config.username, config.password); 
      return new AuthenticationContext(config.tenant) 
       .AcquireToken(config.resource, config.clientAppId, user) 
       .CreateAuthorizationHeader(); 
     } 
    } 
} 

PS : 결과 DLL은 3.5 .NET 프레임 워크에서 실행되는 응용 프로그램에서 플러그인으로 가져온 최신 프레임 워크 컴파일 할 수 없습니다. 그래서 그것은 효과가 없을 것입니다.

Ps : .CreateAuthorizationHeader()은 단지 "Bearer " + AccessToken을 반환한다는 것을 알고 있습니다. 그래서 그게 문제가 아니에요. AccessToken을 얻으려면.

답변

0

결국, AcquireToken은 https 요청을 STS로 보냅니다. 이 시뮬레이션을 직접 쉽게 할 수 있습니다. 요청 (AAD 용)과 같이 간다 :이 웹 클라이언트 (체육 시간, How to fill forms and submit with Webclient in C#)을 쉽게 수행 할

POST https://login.microsoftonline.com/your-tenant-id/oauth2/token HTTP/1.1 
Accept: application/json 
x-client-Ver: 3.13.5.907 
x-client-CPU: x64 
x-client-OS: Microsoft Windows NT 6.2.9200.0 
x-ms-PKeyAuth: 1.0 
client-request-id: 10a9f6d3-1247-493e-874f-fab04e1427c7 
return-client-request-id: true 
Content-Type: application/x-www-form-urlencoded 
Host: login.microsoftonline.com 
Content-Length: 183 
Expect: 100-continue 
Connection: Keep-Alive 

resource=your-resource-guid&client_id=your-lcient-guid&client_secret=***** CREDENTIALS REMOVED HERE *****&grant_type=client_credentials 

.

HTTP/1.1 200 OK 
Cache-Control: no-cache, no-store 
... 
Content-Type: application/json; charset=utf-8 
Expires: -1 
Server: Microsoft-IIS/8.5 
Strict-Transport-Security: max-age=31536000; includeSubDomains 
X-Content-Type-Options: nosniff 
client-request-id: 10a9f6d3-1247-493e-874f-fab04e1427c7 
x-ms-request-id: bla-bla 
… 
X-Powered-By: ASP.NET 
Date: Thu, 23 Feb 2017 08:35:26 GMT 
Content-Length: 1278 

{"token_type":"Bearer","expires_in":"3599","ext_expires_in":"10800","expires_on":"1487842528","not_before":"1487838628","resource":"your-resource-id","access_token":"your-access-token"} 

결과는 JSON이고 토큰은 "access_token이"필드에 : 서버의 응답은 일반적으로 이런 일이 될 것입니다. 피들러 (Fiddler)와 같은 도구를 사용하여 요청을 올바르게 처리 할 수는 있지만 기본적으로 피들러 (Fiddler)와 관련이 있습니다. (당신은 아마도 뉴손 소프트를 사용하여 json을 적절히 비 직렬화 할 것입니다.)

그것이 제게 모든 것이 당신을 위해하는 것은 아닙니다. ADAL은 또한 토큰 캐싱과 같은 작업을 수행하므로 각 통화마다 토큰을 요청할 필요가 없으며 만료일을 자동으로 처리합니다.하지만 약간의 코드만으로도 자신을 굴릴 수 있습니다. 희망이 도움이됩니다.