我正试图通过SSL提出一个请求。证书已经安装在机器上,并通过浏览器工作。
我利用这一请求:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = encoding.GetBytes(request.Content.OuterXml.ToString());
string password = "XXXX";
X509Certificate2 cert = new X509Certificate2("c:\\zzzz.p12", password);
string key = cert.GetPublicKeyString();
string certData = Encoding.ASCII.GetString(cert.Export(X509ContentType.Cert));
Uri uri = new Uri(request.Url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
myRequest.Credentials = new NetworkCredential(request.User, request.Password.ToString());
myRequest.Method = "PUT";
myRequest.ContentType = request.ContentType;
myRequest.ContentLength = data.Length;
myRequest.ClientCertificates.Add(cert);
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
System.IO.StreamReader st = new StreamReader(((HttpWebResponse)myRequest.GetResponse()).GetResponseStream());
使用这段代码,我得到了以下错误:
基础连接已关闭:无法为SSL/TLS安全通道建立信任关系。-> System.Security.Authentication.AuthenticationException:根据验证过程远程证书无效。
有什么问题吗?
发布于 2012-06-28 08:57:51
我用这个解决了问题:
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate { return true; }
);
发布于 2016-12-01 11:07:30
确保您的证书是正确可信的。根证书是否已添加到正确的证书存储区(本地机器上的可信根CA)?
当将(自签名的)证书的根证书添加到当前用户的受信任根CA时,我遇到了此错误。将根证书移动到本地机器上的根CA存储解决了我的问题。
发布于 2012-06-27 09:10:39
这是客户代码对吧?你正在访问一个HTTPS url,不是吗?我认为问题在于服务器证书,客户端TLS堆栈无法验证该证书。当您通过浏览器连接到该URL时,它是否正常工作,还是看到了证书不匹配的警告?
https://stackoverflow.com/questions/11222759
复制相似问题