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?
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.
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.
I solved the problem with this:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback ( delegate { return true; } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With