我們的科爾多瓦應用程序需要從本地ADFS(3.0)請求安全令牌。然後使用該令牌連接到Web服務。我發現的所有例子都說這是可能的,但只能演示如何使用Azure來做到這一點。ADFS 3.0 +科爾多瓦移動應用程序+ API
從哪裏可以找到有關配置ADFS 3.0的詳細信息?有更好的方法嗎?
我們的科爾多瓦應用程序需要從本地ADFS(3.0)請求安全令牌。然後使用該令牌連接到Web服務。我發現的所有例子都說這是可能的,但只能演示如何使用Azure來做到這一點。ADFS 3.0 +科爾多瓦移動應用程序+ API
從哪裏可以找到有關配置ADFS 3.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交互根本無法工作。只保存你的頭痛。