C#:无法在IIS

时间:2018-06-27 06:11:56

标签: c# ssl

我有一个从Internet Explorer导出为.cer文件的证书,我使用它通过c#连接一个网站,使用iis express在Visual Studio上的连接工作正常,但是切换到IIS时返回错误。

错误:无法创建SSL / TLS安全通道,并返回一段时间:无法从传输连接中读取数据:远程主机强行关闭了现有连接

那么为什么它在iis express上不能正常工作?

备注:证明文件没有私钥。

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

        }

0 个答案:

没有答案