Skip to content

Instantly share code, notes, and snippets.

@korteke
Created March 11, 2016 14:26
Show Gist options
  • Select an option

  • Save korteke/f3f9f62b190a781ad643 to your computer and use it in GitHub Desktop.

Select an option

Save korteke/f3f9f62b190a781ad643 to your computer and use it in GitHub Desktop.
First the actual code that tries to crete request. Second method that creates webRequest. Third snippet from the .NET tracing.
System.Net Verbose: 0 : [4612] 00000000 : 3C 3F 78 6D 6C 20 76 65-72 73 69 6F 6E 3D 22 31 : <?xml version="1
System.Net Verbose: 0 : [4612] 00000010 : 2E 30 22 20 65 6E 63 6F-64 69 6E 67 3D 22 55 54 : .0" encoding="UT
System.Net Verbose: 0 : [4612] 00000020 : 46 2D 38 22 3F 3E 0D 0A-3C 53 4F 41 50 2D 45 4E : F-8"?>..<SOAP-EN
System.Net Verbose: 0 : [4612] 00000030 : 56 3A 45 6E 76 65 6C 6F-70 65 20 78 6D 6C 6E 73 : V:Envelope xmlns
System.Net Verbose: 0 : [4612] 00000040 : 3A 53 4F 41 50 2D 45 4E-56 3D 22 68 74 74 70 3A : :SOAP-ENV="http:
System.Net Verbose: 0 : [4612] 00000050 : 2F 2F 73 63 68 65 6D 61-73 2E 78 6D 6C 73 6F 61 : //schemas.xmlsoa
System.Net Verbose: 0 : [4612] 00000060 : 70 2E 6F 72 67 2F 73 6F-61 70 2F 65 6E 76 65 6C : p.org/soap/envel
System.Net Verbose: 0 : [4612] 00000070 : 6F 70 65 2F 22 3E 0D 0A-20 20 3C 53 4F 41 50 2D : ope/">.. <SOAP-
System.Net Verbose: 0 : [4612] 00000080 : 45 4E 56 3A 42 6F 64 79-3E 0D 0A 20 20 20 20 3C : ENV:Body>.. <
System.Net Verbose: 0 : [4612] 00000090 : 73 61 6D 6C 70 3A 41 74-74 72 69 62 75 74 65 51 : samlp:AttributeQ
System.Net Verbose: 0 : [4612] 000000A0 : 75 65 72 79 20 49 44 3D-22 5F 33 42 37 39 42 44 : uery ID="_3B79BD
System.Net Verbose: 0 : [4612] 000000B0 : 32 43 35 33 42 39 41 31-38 42 32 46 36 44 41 45 : 2C53B9A18B2F6DAE
System.Net Verbose: 0 : [4612] 000000C0 : 42 42 45 34 36 39 33 42-41 45 22 20 56 65 72 73 : BBE4693BAE" Vers
System.Net Verbose: 0 : [4612] 000000D0 : 69 6F 6E 3D 22 32 2E 30-22 20 49 73 73 75 65 49 : ion="2.0" IssueI
System.Net Verbose: 0 : [4612] 000000E0 : 6E 73 74 61 6E 74 3D 22-32 30 31 36 2D 30 33 2D : nstant="2016-03-
System.Net Verbose: 0 : [4612] 000000F0 : 31 31 54 31 31 3A 34 37-3A 35 37 2E 37 39 34 5A : 11T11:47:57.794Z
System.Net Verbose: 0 : [4612] 00000100 : 22 20 78 6D 6C 6E 73 3A-73 61 6D 6C 70 3D 22 75 : " xmlns:samlp="u
System.Net Verbose: 0 : [4612] 00000110 : 72 6E 3A 6F 61 73 69 73-3A 6E 61 6D 65 73 3A 74 : rn:oasis:names:t
System.Net Verbose: 0 : [4612] 00000120 : 63 3A 53 41 4D 4C 3A 32-2E 30 3A 70 72 6F 74 6F : c:SAML:2.0:proto
System.Net Verbose: 0 : [4612] 00000130 : 63 6F 6C 22 3E 0D 0A 20-20 20 20 20 20 3C 73 61 : col">.. <sa
System.Net Verbose: 0 : [4612] 00000140 : 6D 6C 3A 49 73 73 75 65-72 20 78 6D 6C 6E
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
try
{
string aaURL = "https://idp.testshib.org:8443/idp/profile/SAML2/SOAP/AttributeQuery";
var _action = "http://www.oasis-open.org/committees/security";
AttributeQuery attributeQuery = new AttributeQuery();
attributeQuery.Destination = aaURL;
attributeQuery.Issuer = new Issuer(Global.entityId);
attributeQuery.Attributes.Add(new ComponentPro.Saml2.Attribute() { Name = "urn:oid:2.5.4.3" });
attributeQuery.Subject = new Subject(samlAssertion.Subject.NameId);
attributeQuery.Sign(x509Certificate);
System.Diagnostics.Trace.WriteLine("Trying to get attributes from AA");
System.Diagnostics.Trace.WriteLine("AA query " + attributeQuery.GetXml().OuterXml);
System.Diagnostics.Trace.WriteLine("AA Subject " + attributeQuery.Subject.ToString());
// This is probably the most horrible way to wrap that attributeQuery to the envelope, but let's try
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = false;
xdoc.LoadXml("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>");
XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
xfrag.InnerXml = @attributeQuery.GetXml().OuterXml;
xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
System.Diagnostics.Trace.WriteLine("SOAP TEST: " + xdoc.OuterXml);
// Canonicalization test
XmlDsigC14NTransform c14n = new XmlDsigC14NTransform();
c14n.LoadInput(xdoc);
Stream s1 = (Stream)c14n.GetOutput(typeof(Stream));
XmlDocument xmldoc2 = new XmlDocument
{
PreserveWhitespace = false
};
xmldoc2.Load(s1);
System.Diagnostics.Trace.WriteLine("SOAP TEST2: " + xmldoc2.OuterXml);
//AA query
HttpWebRequest webRequest = CreateWebRequest(aaURL, _action);
InsertSoapEnvelopeIntoWebRequest(xmldoc2, webRequest);
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed soap request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
System.Diagnostics.Trace.WriteLine("SOAP result: " + soapResult);
}
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine("Execption: " + e.ToString());
//throw;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment