0

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();

        }
  • Try with this: https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f – freshbm Jun 27 '18 at 06:14
  • Thanks for your response freshbm, i tried all the suggestions in the web, but all fails, i think it is a permission issue, but i can not know how to fix it. all the solutions in the web is to give grant access using winhttpcertcfg but it is not working because missing private key and need .pfx file. – Jawdat SOBH Jun 27 '18 at 06:26

0 Answers0