2016-09-05 96 views
0

我從ADFS獲得JSON Web令牌併成功調用Web API函數。有效。但是,我無法將代碼轉換爲Windows 10 Native或通用應用程序。使用JWT的ADFS和本地應用程序身份驗證

任何人都知道該怎麼辦呢?我的下面的代碼在控制檯應用程序中完美工作

我沒有任何想法如何納入本機應用程序WSTrustChannelFactory。另外,下面的(GenericXmlSecurityToken).TokenXml也有問題。

var token = GenericXmlSecurityToken GetJWT(); 
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.TokenXml.OuterXml); 

或者更適合移動應用的想法?

謝謝

private static GenericXmlSecurityToken GetJWT() 
{ 

     //string endpointUri = string.Format("https://{0}/adfs/services/trust/13/usernamemixed", _serverName); 
     string endpointUri = string.Format("https://{0}/adfs/services/trust/2005/usernamemixed", _serverName); 

     var factory = new WSTrustChannelFactory(
         new UserNameWSTrustBinding(), 
         new EndpointAddress(endpointUri)); 

     //factory.TrustVersion = TrustVersion.WSTrust13; 
     factory.TrustVersion = TrustVersion.WSTrustFeb2005; 

     if (factory.Credentials != null) 
     { 
      factory.Credentials.UserName.UserName = _userName; 
      factory.Credentials.UserName.Password = _password; 
     } 

     var rst = new RequestSecurityToken 
     { 
      RequestType = RequestTypes.Issue, 
      KeyType = KeyTypes.Bearer, 
      AppliesTo = new EndpointReference(_relyingPartyUri), 
      KeySizeInBits = 0, 

      TokenType = "urn:ietf:params:oauth:token-type:jwt", 

     }; 

     var channel = factory.CreateChannel(); 
     try 
     { 
      var token = channel.Issue(rst); 
      return token as GenericXmlSecurityToken; 
     } 
     catch (ProtocolException ex) 
     { 
      Debug.Write(ex.Message); 
     } 
     return null; 
} 

回答

0

我沒有任何想法如何包括WSTrustChannelFactory在本機應用程序

最好的做法是避免將這種方法應用UWP,它應該將其包裝在Web服務中並將安全令牌返回給我們的UWP應用程序。

enter image description here

如果你是側載(你不需要發佈到微軟商店),則可以考慮增加一個牽線的Windows運行時組件或桌面應用程序(控制檯/ WinForm的/ WPF ...)作爲經紀人服務器

Brokered Windows Runtime Components for side-loaded Windows Store apps

0

它應該是類似於本地WPF應用程序。而不是使用WSTrustChannelFactory,請使用ADAL v2.28

集權威性,resourceURI,客戶端ID,並通過returnUri。然後使用它們來創建一個AuthenticationContext並請求一個令牌。最後,將授權標頭填充到授權標頭中,並將請求發送到API。

在API側手動驗證令牌,並設置爲ClaimsPrincipal保護控制器&方法。

當我有幾分鐘,我將修改它並添加一些鏈接&示例代碼。現在請參考這個例子。

ADAL Native Client Example