Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: C# The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

I'm trying to make a request via SSL. The certificate is already installed on the machine and it works via browser.

I am using this request:

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

Using this code I get this error:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

What is the problem?

like image 970
Roger G Avatar asked Jun 27 '12 09:06

Roger G


People also ask

What is an error C?

Errors in C language is defined as an illegal operation performed by the user which will result in the abnormal or abrupt working of the program logic. Programming errors are unidentified until the program is compiled or executed. Some of the errors in C are hidden or prevent the program from compiled or executed.

What is logical error in C?

A logic error (or logical error) is a 'bug' or mistake in a program's source code that results in incorrect or unexpected behaviour. It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running. Many different types of programming mistakes can cause logic errors.


1 Answers

I solved the problem with this:

ServicePointManager.ServerCertificateValidationCallback = new         RemoteCertificateValidationCallback (    delegate { return true; } ); 
like image 125
Roger G Avatar answered Sep 28 '22 02:09

Roger G