131 lines
4.5 KiB
C#
131 lines
4.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Xml;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.EESZTInterface.STS
|
|
{
|
|
public class DoRequest
|
|
{
|
|
public static XmlDocument GetSoapSamlResponse(string stsUri, X509Certificate2 sslAuthCert, XmlDocument soapSamlRequest)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest request = GetHttpWebRequest(stsUri, sslAuthCert);
|
|
var response = WriteToHttpStream(request, soapSamlRequest);
|
|
var soapResponse = ThrowIfSoapFault(response, stsUri);
|
|
|
|
return soapResponse;
|
|
|
|
}
|
|
catch (WebException wex)
|
|
{
|
|
string exMessage = wex.Message;
|
|
|
|
if (wex.Response != null)
|
|
{
|
|
using (var responseReader = new StreamReader(wex.Response.GetResponseStream()))
|
|
{
|
|
exMessage = responseReader.ReadToEnd();
|
|
ThrowIfSoapFault(exMessage, wex.Response.ResponseUri.OriginalString);
|
|
}
|
|
}
|
|
throw new Exception(exMessage, wex);
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private static HttpWebRequest GetHttpWebRequest(string stsUri, X509Certificate2 sslAuthCert)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(stsUri);
|
|
request.Headers.Add("SOAPAction", STSValues.soapActionValue);
|
|
request.ContentType = "application/soap+xml;charset=\"utf-8\"";
|
|
request.Accept = "application/soap+xml";
|
|
request.Method = "POST";
|
|
request.ClientCertificates.Add(sslAuthCert);
|
|
return request;
|
|
}
|
|
|
|
private static string WriteToHttpStream(HttpWebRequest request, XmlDocument samlRequest)
|
|
{
|
|
var outputStream = new MemoryStream();
|
|
samlRequest.Save(outputStream);
|
|
|
|
request.ContentLength = outputStream.Length;
|
|
outputStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
var outStr = outputStream.ToArray();
|
|
outputStream.Close();
|
|
|
|
using (Stream requestStream = request.GetRequestStream())
|
|
{
|
|
using (StreamWriter stmw = new StreamWriter(requestStream))
|
|
{
|
|
requestStream.Write(outStr, 0, outStr.Length);
|
|
}
|
|
}
|
|
|
|
string responseString = string.Empty;
|
|
WebResponse response = request.GetResponse();
|
|
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
|
|
{
|
|
responseString = sr.ReadToEnd();
|
|
}
|
|
return responseString;
|
|
|
|
}
|
|
|
|
private static XmlDocument ThrowIfSoapFault(string response, string responseUri)
|
|
{
|
|
XmlDocument soapMessage = null;
|
|
|
|
try
|
|
{
|
|
soapMessage = ExtractDocumentFromResponse(response);
|
|
|
|
if (soapMessage == null)
|
|
{
|
|
throw new Exception();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(string.Format(EESZTInterfaceResource.NemMegfeleloSOAPValaszFormatum, responseUri, response), ex);
|
|
}
|
|
|
|
XmlElement fault = XmlHelper.GetElement("Fault", Namespaces.soap12Ns, soapMessage.DocumentElement);
|
|
if (fault == null)
|
|
{
|
|
return soapMessage;
|
|
}
|
|
|
|
var faultCodeNodes = XmlHelper.GetElement("Code", Namespaces.soap12Ns, fault);
|
|
var faultReasonNodes = XmlHelper.GetElement("Reason", Namespaces.soap12Ns, fault);
|
|
var exceptionDetailNodes = XmlHelper.GetElement("Detail", Namespaces.soap12Ns, fault);
|
|
|
|
throw new Exception("Hiba! Kód:" + faultCodeNodes.InnerText + " Részletek:" + faultReasonNodes.InnerText,
|
|
new Exception((exceptionDetailNodes == null) ? "" : exceptionDetailNodes.InnerText));
|
|
}
|
|
|
|
private static XmlDocument ExtractDocumentFromResponse(string response)
|
|
{
|
|
var soapResponse = new XmlDocument();
|
|
soapResponse.PreserveWhitespace = true;
|
|
soapResponse.LoadXml(response);
|
|
|
|
return soapResponse;
|
|
}
|
|
|
|
private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|