我在使用SUstainsys.SAaml2
库的 CALLback url 中使用此代码:
public ActionResult Callback()
{
var samlToken = (Saml2SecurityToken)null;
var samlResponses = HttpContext.Request.Form["SAMLResponse"]; HttpContext.Current.Request.Form["SAMLResponse"]; or elsewhere.
//if (form.Count() > 0)
//{
// var samlResponses = form.GetValues("SAMLResponse");
if (samlResponses != null)
{
foreach (var samlResponse in samlResponses)
{
try
{
var decodedSamlResponse = Convert.FromBase64String(samlResponse.ToString());
var reader = XmlReader.Create(new MemoryStream(decodedSamlResponse));
var serializer = new XmlSerializer(typeof(XmlElement));
var samlResponseElement = (XmlElement)serializer.Deserialize(reader);
var manager = new XmlNamespaceManager(samlResponseElement.OwnerDocument.NameTable);
manager.AddNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
var assertion = (XmlElement)samlResponseElement.SelectSingleNode("//saml2:Assertion", manager);
//var samltoken= Options.FromConfiguration.SPOptions.Saml2PSecurityTokenHandler.ReadToken(XmlReader.Create(new StringReader(assertion.OuterXml)));
samlToken = (Saml2SecurityToken)Options.FromConfiguration.SPOptions.Saml2PSecurityTokenHandler.ReadToken(XmlReader.Create(new StringReader(assertion.OuterXml)));
break;
}
catch { }
}
}
ViewBag.SamlResponse = samlResponses;
ViewBag.SamlToken = samlToken;
return View();
}
但我在 SAML 响应中得到空值。