502 error when generating X509Certificate2 from p12 certificate in Azure Websites for Google API 502 error when generating X509Certificate2 from p12 certificate in Azure Websites for Google API asp.net asp.net

502 error when generating X509Certificate2 from p12 certificate in Azure Websites for Google API


I found the solution on this MSDN forum post. Basically I needed to set X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet as I read in the first SO post I mentioned and then I needed Flags = CspProviderFlags.UseMachineKeyStore in my CspParamaters.

I have posted my full solution on GitHub


Firstly, you should add the p12 file to resources:

Right click on project - properties - resources - add resource and after that use this line of code for the certificate

var certificate = new X509Certificate2((byte[])Properties.Resources.ResourceManager.GetObject(ResourceName), "!password!", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);