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.
https://msdn.microsoft.com/en-us/library/windowsazure/ee460782.aspx
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 ourHttpWebRequest 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);
certStore.Open(OpenFlags.ReadOnly);
// Find the certificate with the specified thumbprint.
certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false);
// Close the certificate store.
certStore.Close();
// 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).
"?9401b008b0fd45158fbf70696fe7e0862ac12e67";
It will not be visible, unless you use some hex editor to check the thumbprint.
You can download notepad++ from https://notepad-plus-plus.org/download/v6.0.html.
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'))
stringbuilder.Append(inputstr[count]);
}
return stringbuilder.ToString();
}
// Call the function to remove special character.
thumbPrint = RemoveSpecialCharacter(thumbPrint);
=============================================================================================================