Windows Azure API | Attaching a Certificate to a Management Service Request

The below link demonstrates how to authenticate your requests for the management service using C# and the System.Net and System.Security.Cryptography.X509Certificates libraries.

Above link contains a sample code which opens the local certificate storenamed “My” and finds the certificate that matches the thumbprint value. Then, it attaches the matching certificate to the request by adding it to the ClientCertificates collection of our HttpWebRequest object.

           // This certificate would have been previously added as a management cert within the Windows Azure management portal.

        string thumbPrint = "your_certificate_thumbprint";

           // Open the certificate store for the current user.

        X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);


     // Find the certificate with the specified thumbprint.

                certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false);

     // Close the certificate store.


     // Check to see if a matching certificate was found.

        if (0 == certCollection.Count) {   

                      throw new Exception("No certificate found containing thumbprint " + thumbPrint);


Although certificate exists and you have provided the correct thumbprint, sometimes the code breaks and will not be able to find the certificate on your machine.  


There is no visible difference in the thumbprints or the code base, but the Find method fails to locate the certificate (in the former code snippet).

This issue is due to a special character (“?”) in the thumbprint which gets appended when you copy the thumbprint from the certificate (as shown below).



It will not be visible, unless you use some hex editor to check the thumbprint.

You can download notepad++ from

Verify the thumbprint that is used in your code using notepad++ tool and remove the special character (if it exists).


Alternatively, use the below function which removes the special character from the thumbprint.

       public static string RemoveSpecialCharacter(string inputstr)


           StringBuilder stringbuilder = new StringBuilder();

           for (int count = 0; count < inputstr.Length; count++)


               if ((inputstr[count] >= '0' && inputstr[count] <= '9') || (inputstr[count] >= 'A' && inputstr[count] <= 'z'))



           return stringbuilder.ToString();


// Call the function to remove special character.

thumbPrint = RemoveSpecialCharacter(thumbPrint);


Comments (0)

Skip to main content