I have a certification exported from internet explorer as .cer file, i use it to connect a website using c#, the connection work fine on visual studio using iis express, but return error when switch to IIS.
The error: Could not create SSL/TLS secure channel and some time return : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
So why it is working fine on iis express and not on iis.
Remark: the certification file doesn't have private key.
X509Certificate2Collection certificates = new X509Certificate2Collection();
certificates.Import(this._certName, this._certPass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
byte[] data = null;
if (method == (int)Method.POST)
{
ASCIIEncoding encoding = new ASCIIEncoding();
data = encoding.GetBytes(post);
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._baseUrl + path);
request.Method = method == (int)Method.POST ? "POST" : "GET";
if (method == (int)Method.POST)
{
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
}
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ClientCertificates = certificates;
request.Proxy = WebRequest.DefaultWebProxy;
request.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
if (this._sessionId != null) {
request.Headers.Set("Cookie", this._sessionId);
}
Stream newStream;
if (method == (int)Method.POST)
{
newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}