Hi everyone!
I am trying to connect to a server that requires a client certificate. I connect with httpWebRequest, and in a console App I made in Visual Sudio, it works like a charm!
But when I try it in Unity, it doesn't work. (the 2 are identical)
It throws me this TlsException at the GetResponse:
> TlsException: The authentication or decryption has failed.> Mono.Security.Protocol.Tls.RecordProtocol.ProcessAlert (AlertLevel alertLevel, AlertDescription alertDesc)> Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult)> Rethrow as WebException: Error getting response stream (ReadDone1): ReceiveFailure> System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)> System.Net.HttpWebRequest.GetResponse ()
So, from what I understand, the problem comes from Mono that prevent the authentication for some security reasons. Any idea how to override it?
Here is the code used if it can help:
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
X509Certificate2 clientCertificate = new X509Certificate2(path + ".p12", "password");
byte[] data = Encoding.ASCII.GetBytes("some data".ToCharArray());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
request.ClientCertificates.Add(clientCertificate);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
↧