1

I know that this question was asked many times on Stackoverflow, however I looked at many of the answers, yet I still couldn't find a solution that worked for me.

I have this block of code, in which I am trying to send a request to a server using WebClient

using (var client = new CertificateWebClient()) { var postData = $"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:v3='http://xmldefs.volkswagenag.com/PP/QM/GroupProblemManagementService/V3'><soapenv:Header><To xmlns='http://www.w3.org/2005/08/addressing'>ws://volkswagenag.com/PP/QM/GroupProblemManagementService/V3</To><Action xmlns='http://www.w3.org/2005/08/addressing'>http://xmldefs.volkswagenag.com/PP/QM/GroupProblemManagementService/V3/KpmService/GetMultipleProblemDataRequest</Action><MessageID xmlns='http://www.w3.org/2005/08/addressing'>${{= \"urn:uuid:\" + UUID.randomUUID()}}</MessageID></soapenv:Header><soapenv:Body><v3:GetMultipleProblemData><UserAuthentification><UserId>{Username}</UserId></UserAuthentification><OverviewAddress><AddressTimestamp/><ContactPerson/><Description/><OrganisationalUnit>KPMEE-05</OrganisationalUnit><Group/><Plant>Z$</Plant></OverviewAddress><ActiveOverview>true</ActiveOverview><PassiveOverview>false</PassiveOverview></v3:GetMultipleProblemData></soapenv:Body></soapenv:Envelope>"; var postArray = Encoding.UTF8.GetBytes(postData); try { var resposneData = Encoding.UTF8 .GetString(client.UploadData("https://ws-gateway-cert.volkswagenag.com/services", postArray)); Debug.WriteLine(resposneData); } catch (WebException we) { var webResponse = (HttpWebResponse)we.Response; if (webResponse != null) { Debug.WriteLine(GetErrorText(webResponse.StatusCode.ToString())); } else { Debug.WriteLine(we); } } } 

Now since the server requires a certificate for authentication, I created a sub class that inherits from WebClient and uses HttpWebRequest to add the certificate.

internal class CertificateWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); string filePath = "../../Resources/Systemuser PAG R_ PAG_KPM_PREH_MFL VWPKI A145CCD2D02592DB.p12"; X509Certificate2 certificate = new X509Certificate2(filePath, ""); //password is not shown in here request.ClientCertificates.Add(certificate); return request; } } 

When starting the program in debug mode, I get the following error:

The request was aborted: Could not create SSL/TLS secure channel.

I tried fixing this problem by setting the SecurityProtocol to Tls1.2, since this is what the server is using, setting Expect100Continue to true and setting the ServerCertificateValidationCallback, as it was suggeested in many responses.

internal class CertificateWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslError) => true; HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); string filePath = "../../Resources/Systemuser PAG R_ PAG_KPM_PREH_MFL VWPKI A145CCD2D02592DB.p12"; X509Certificate2 certificate = new X509Certificate2(filePath, ""); //password is not shown in here request.ClientCertificates.Add(certificate); return request; } } 

That unfortunately couldn't fix the problem either. I know that the certificate is working tho, since I set up a test environment via Soap UI, using the same certificate, the same username and the same URI. And it worked perfectly. I use .NET Framework version 4.8

What am I missing out on?

1 Answer 1

1

I was able to solve the problem by using another Constructor for the X509Certificate2 class. Instead of using X509Certificate2(string, string) I used X509Certificate(string, string, X509KeyStorageFlags)

internal class CertificateWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); string filePath = "../../Resources/Systemuser PAG R_ PAG_KPM_PREH_MFL VWPKI A145CCD2D02592DB.p12"; X509Certificate2 certificate = new X509Certificate2(filePath, "", X509KeyStorageFlags.UserKeySet); //password is not shown here request.ClientCertificates.Add(certificate); return request; } } 

By doing so, the server accepted the certificate and the request was successful.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.