2016-10-13 47 views
-1

我们的科尔多瓦应用程序需要从本地ADFS(3.0)请求安全令牌。然后使用该令牌连接到Web服务。我发现的所有例子都说这是可能的,但只能演示如何使用Azure来做到这一点。ADFS 3.0 +科尔多瓦移动应用程序+ API

从哪里可以找到有关配置ADFS 3.0的详细信息?有更好的方法吗?

回答

0

我今天解决了这个问题。这是一个工作示例。它应该适用于所有移动应用程序,而不仅仅是Cordova。

 string adfsHost = "https://<Your ADFS FQDN>"; 
     string sendTo = $"{adfsHost}/adfs/services/trust/13/usernamemixed"; 
     string _username = "<Your Domain\\<Your username>"; 
     string _password = "<Your password>"; 
     string applyTo = "<Your Resource URI>"; 
     string tokenType = "urn:ietf:params:oauth:token-type:jwt"; 

     string soapMessage = [email protected]" 
      <s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"" 
         xmlns:a=""http://www.w3.org/2005/08/addressing"" 
         xmlns:u=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""> 
       <s:Header> 
       <a:Action s:mustUnderstand=""1"">http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</a:Action> 
       <a:To s:mustUnderstand=""1"">{sendTo}</a:To> 
       <o:Security s:mustUnderstand=""1"" xmlns:o=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""> 
        <o:UsernameToken u:Id="" uuid-00000000-0000-0000-0000-000000000000-0""> 
        <o:Username>{_username}</o:Username> 
        <o:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">{_password}</o:Password> 
        </o:UsernameToken> 
       </o:Security> 
       </s:Header> 
       <s:Body> 
       <trust:RequestSecurityToken xmlns:trust=""http://docs.oasis-open.org/ws-sx/ws-trust/200512""> 
        <wsp:AppliesTo xmlns:wsp=""http://schemas.xmlsoap.org/ws/2004/09/policy""> 
        <a:EndpointReference> 
         <a:Address>{applyTo}</a:Address> 
        </a:EndpointReference> 
        </wsp:AppliesTo> 
        <trust:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer</trust:KeyType> 
        <trust:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</trust:RequestType> 
        <trust:TokenType>{tokenType}</trust:TokenType> 
       </trust:RequestSecurityToken> 
       </s:Body> 
      </s:Envelope> 
     "; 

     XmlDocument xml = new XmlDocument(); 
     xml.LoadXml(soapMessage); 

     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sendTo); 

     request.ContentType = "application/soap+xml; charset=utf-8"; 
     request.Method = "POST"; 
     request.Accept = "application/json"; 

     var stream = request.GetRequestStream(); 
     xml.Save(stream); 

     WebResponse response = request.GetResponse(); 
     string strResponse; 
     using (Stream responseStream = response.GetResponseStream()) 
     { 
      using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII)) 
      { 
       strResponse = sr.ReadToEnd(); 
      } 
     } 

     JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); 

     XmlDocument xml2 = new XmlDocument(); 
     xml2.LoadXml(strResponse); 

     XmlNode node = xml2.SelectSingleNode("//*[@ValueType='urn:ietf:params:oauth:token-type:jwt']"); 
     XmlReader reader = new XmlNodeReader(node); 
     JwtSecurityToken token = (JwtSecurityToken)handler.ReadToken(reader); 

     string encryptedToken = token.RawData; 

该代码模拟从顶部的移动应用程序接收用户凭证。然后它建立了调用ADFS 3.0所需的其余值。这些值使用字符串插值插入到SOAP信封中。

接下来,它创建一个Web请求。然后它将SOAP信封添加到请求并调用ADFS端点。您应该收到一个包含BinarySecurityToken和状态代码200的SOAP响应。

JWT令牌包装在BinarySecurityToken中。要知道它,你必须选择包含它的wsse:BinarySecurityToken,并使用JwtSecurityTokenHandler.ReadToken()将其拉出。然后,您可以将令牌发送到可用于完成API请求的移动应用程序。

您可以使用相同的方法直接从手机调用ADFS,但我更喜欢在API端做这件事。

此外,我强烈建议您不要使用自签名证书。恕我直言,许多ADFS交互根本无法工作。只保存你的头痛。

相关问题