【发布时间】:2016-10-13 16:40:41
【问题描述】:
我们的 Cordova 应用需要从本地 ADFS (3.0) 请求安全令牌。然后使用令牌连接到 Web 服务。我发现的所有示例都表明这是可能的,但仅演示了如何使用 Azure 来实现。
在哪里可以找到配置 ADFS 3.0 的详细信息?是否存在更好的方法?
【问题讨论】:
我们的 Cordova 应用需要从本地 ADFS (3.0) 请求安全令牌。然后使用令牌连接到 Web 服务。我发现的所有示例都表明这是可能的,但仅演示了如何使用 Azure 来实现。
在哪里可以找到配置 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 = $@"
<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 交互根本不起作用。省得自己头疼。
【讨论】: