我尝试使用WIF 4.5和WS-Trust从ADFS获取SAML断言,以便我可以将该断言发送给服务提供商并获取OAuth票证。
事实上,我已经能够获得SAML断言,但它不是有效的,因为没有收到来自SubjectConfirmationData的Recipient属性。这是一个强制性数据。
我在控制台应用程序中进行测试(所以它使用我的凭据执行,因为我使用Fiddler进行了检查,它在接收断言之前执行Kerberos协商)。我正在获取令牌(基于RequestSecurityToken using windows credentials and .net 4.5 WIF):
public static string GetStsToken()
{
try
{
EndpointReference appliesToEp = new EndpointReference(ENDPOINT_REFERENCE_URI);
EndpointAddress stsEp = new EndpointAddress(
new Uri("https://<ADFS-SERVER>/adfs/services/trust/2005/windowstransport"),
EndpointIdentity.CreateSpnIdentity(ADFS_SPN));
WS2007HttpBinding msgBinding = new WS2007HttpBinding(SecurityMode.Transport, false);
msgBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
msgBinding.Security.Message.EstablishSecurityContext = false;
msgBinding.Security.Message.NegotiateServiceCredential = false;
msgBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
using (WSTrustChannelFactory factory = new WSTrustChannelFactory(msgBinding, stsEp))
{
factory.Credentials.SupportInteractive = false;
factory.TrustVersion = TrustVersion.WSTrustFeb2005;
RequestSecurityToken myRst =
new RequestSecurityToken(RequestTypes.Issue, KeyTypes.Bearer)
{
AppliesTo = appliesToEp,
TokenType = "urn:oasis:names:tc:SAML:2.0:assertion"
};
IWSTrustChannelContract channel = factory.CreateChannel();
GenericXmlSecurityToken stsToken = channel.Issue(myRst) as GenericXmlSecurityToken;
if (stsToken != null)
{
return stsToken.TokenXml.OuterXml;
}
else
{
// SOME WARNING IS ISSUED
}
}
}
catch (Exception ex)
{
// THE EXCEPTION IS REGISTERED
}
return null;
}
使用此代码,发送的请求如下:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
<a:MessageID>urn:uuid:8c221169-52b2-42bf-87f8-7089b6feb0a9</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://ADFS-SERVER/adfs/services/trust/2005/windowstransport</a:To>
</s:Header>
<s:Body>
<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
<wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Address>ENDPOINT_REFERENCE</wsa:Address>
</wsa:EndpointReference>
</wsp:AppliesTo>
<t:KeySize>0</t:KeySize>
<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>
<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
<t:TokenType>urn:oasis:names:tc:SAML:2.0:assertion</t:TokenType>
</t:RequestSecurityToken>
</s:Body>
</s:Envelope>
我收到的断言似乎是有效的,但是SubjectConfirmationData中缺少收件人,因此,当我将该断言发送给服务提供商时,身份验证失败。
如果我使用向服务器发送samlp:AuthnRequest
的Web IdP启动登录,并解码获得的ADFS在这种情况下发出的SAML断言(再次使用Fiddler),则会收到“收件人”属性,并且SSO的工作原理。我可以看到用于获取断言的方法是不同的(在这种情况下使用Web-SSO),但在两种情况下依赖方都是相同的,因此发出的断言应该是相似的。
在使用ADFS的WS-Trust获取令牌时,有什么方法可以获得正确的收件人吗?
答案 0 :(得分:1)
最后,我结束了使用IdP Initiated Sign On页面获取SAML断言。
public static string GetStsToken(string relyingPartyUri)
{
string result = null;
string samlHttpPostUri = string.Format(
"https://<ADFS-SERVER>/adfs/ls/idpinitiatedsignon.aspx?loginToRp={0}",
relyingPartyUri
);
WebRequest req = WebRequest.Create(samlHttpPostUri);
req.Method = WebRequestMethods.Http.Get;
req.Credentials = CredentialCache.DefaultNetworkCredentials;
XDocument xDoc = null;
try
{
using (WebResponse resp = req.GetResponse())
{
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
string htmlResult = reader.ReadToEnd();
xDoc = XDocument.Parse(htmlResult);
string samlResponseBase64 = (from xElement in xDoc.Descendants()
where xElement.Name == "input" &&
xElement.Attribute("name").Value == "SAMLResponse"
select xElement.Attribute("value").Value).FirstOrDefault();
result = System.Text.Encoding.UTF8.GetString(
Convert.FromBase64String(samlResponseBase64)
);
}
}
}
catch (WebException webExc)
{
using (StreamReader reader = new StreamReader(webExc.Response.GetResponseStream()))
{
// THE EXCEPTION IS REGISTERED
}
}
return result;
}
SAML响应是正确的,我可以使用它来从依赖方获取OAuth令牌。
但是,当然,我没有在这种情况下使用WS-Trust 。这不是解决方案,而是解决方法。