How to troubleshot HttpWebRequest if X509Certificate is not added properly

Kuler Master 266 Reputation points
2024-11-04T10:19:14.1133333+00:00

Hello all,

I am adding a X509Certificate to the HttpRequest like the following:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("webserviceurl");

request.ClientCertificates.Clear(); 

X509Certificate certificate = new X509Certificate(
    certificateFilePath, certificatePassword, 
    X509KeyStorageFlags.MachineKeySet | 
    X509KeyStorageFlags.PersistKeySet | 
    X509KeyStorageFlags.Exportable);

if(certificate != null) 
    request.ClientCertificates.Add(certificate);

However, sometimes the certificate is not added correctly or is blocked by something (ISP, Firewall etc. etc.) and we get an error from the web service we send it toward. It happens like 3 times out of 10. Meaning it works 3 times, then this problem occurs or works 5 times and then happens 2 times in a row - no specific pattern of when it happens. How do I troubleshot and figure out what could be the reason for this?

Thank you in advance!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,646 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,522 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,064 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,743 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,406 Reputation points
    2024-11-12T18:05:31.48+00:00

    as you are creating a new certificate on each request, it unclear why you persist the keys.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.