private async Task <OnHttpRequestArguments> RunBeforeHttpRequestHandlers(string xml, string url, string action, Guid trackingId, object state, IEnumerable <ISoapHandler> orderedHandlers, CancellationToken ct) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, url); var beforeHttpRequestArguments = new OnHttpRequestArguments(requestMessage, url, action, trackingId) { State = state, Request = { Content = new StringContent(xml, Encoding.UTF8) } }; beforeHttpRequestArguments.Request.Content.Headers.Clear(); var contentTypeValue = $"application/soap+xml;charset=UTF-8;action=\"{action}\""; if (!beforeHttpRequestArguments.Request.Content.Headers.TryAddWithoutValidation("Content-Type", contentTypeValue)) { throw new SoapClientException($"Could not assign '{contentTypeValue}' as the 'Content-Type' header value"); } foreach (var handler in orderedHandlers) { await handler.OnHttpRequestAsync(this, beforeHttpRequestArguments, ct); handler.OnHttpRequest(this, beforeHttpRequestArguments); } return(beforeHttpRequestArguments); }
private static async Task OnSoapEnvelopeRequest( OnHttpRequestArguments arguments, X509Certificate2 x509Certificate2) { var xmlString = await arguments.Request.Content.ReadAsStringAsync(); var document = new XmlDocument(); document.PreserveWhitespace = true; document.LoadXml(xmlString); const string soapNs = "http://schemas.xmlsoap.org/soap/envelope/"; var body = (XmlElement)document.DocumentElement.GetElementsByTagName("Body", soapNs).Item(0); var bodyId = "id-" + Guid.NewGuid().ToString("N"); // Id have to be added twice because SignedXml cannot find element with wsu namespace var wsuId = document.CreateAttribute("wsu", "Id", wsuNs); wsuId.Value = bodyId; body.Attributes.Append(wsuId); var id = document.CreateAttribute("Id"); id.Value = bodyId; body.Attributes.Append(id); var header = (XmlElement)document.DocumentElement.GetElementsByTagName("Header", soapNs).Item(0); Sign(document, bodyId, header, x509Certificate2); var textWiter = new StringWriter(); using (var writer = new XmlTextWriter(textWiter)) { document.WriteTo(writer); writer.Flush(); xmlString = textWiter.ToString(); } arguments.Request.Content = new StringContent(xmlString); }
private async Task <OnHttpRequestArguments> RunBeforeHttpRequestHandlers( string xml, string url, string action, Guid trackingId, object state, ISoapHandler[] orderedHandlers, Constant.SoapEnvelopeVersion version, CancellationToken ct) { var beforeHttpRequestArguments = new OnHttpRequestArguments(new HttpRequestMessage(HttpMethod.Post, url), url, action, trackingId) { State = state }; switch (version) { case Constant.SoapEnvelopeVersion.Undefined: case Constant.SoapEnvelopeVersion.V1Dot1: beforeHttpRequestArguments.Request.Content = new StringContent(xml, Encoding.UTF8, "text/xml"); beforeHttpRequestArguments.Request.Headers.Add("SOAPAction", action); break; case Constant.SoapEnvelopeVersion.V1Dot2: beforeHttpRequestArguments.Request.Content = new StringContent(xml, Encoding.UTF8); var contentTypeValue = string.Concat("application/soap+xml;charset=UTF-8;action=\"", action, "\""); if (!beforeHttpRequestArguments.Request.Content.Headers.TryAddWithoutValidation( "Content-Type", contentTypeValue)) { throw new SoapClientException( $"Could not assign '{contentTypeValue}' as the 'Content-Type' header value"); } break; default: throw new ArgumentOutOfRangeException(nameof(version), version, null); } foreach (var handler in orderedHandlers) { await handler.OnHttpRequestAsync(this, beforeHttpRequestArguments, ct); handler.OnHttpRequest(this, beforeHttpRequestArguments); } return(beforeHttpRequestArguments); }