2016-09-05 50 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