Creating X509Certificates from an Azure Web Service results in 'The system cannot find the file specified'

In a project I have worked on we wanted to create in memory certificates to use in our web application.

There are a lot of reasons why you don't want to enable this kind of functionality, but let's keep it
simple and only talk about this scenario without arguing.

In our code we used X509Certificate2 to create the in memory certificate using a byte array (byte[] certificateData) and
a password (string sercurePassword).

return new X509Certificate2(certificateData, securePassword);

To make our live as developers a little simpeler we created both a Web project (IIS Express) and a Console (Selfhost) project in Visual Studio.
During development I mainly use the Selfhost but the above solution works very nice in both the Selfhost as the Web Project. So I created a pull request and my colleages reviewed and completed the pull request. The nice thing about our build/release cycle is that in about 5 minutes the above code was deployed to Azure in de Development
Environment.

WHAT???? It's not working......

After looking in the Error Log I found this error:

"The system cannot find the file specified"

WHAT?? I don't have a file.. so.. which file can't be found...
All the code is only using in memory variables... Help!!!

Luckily I found the answer in one of the stack overflow answers.

Due to the fact that the Azure Web Service does not have a User Profile it cannot create a Certificate for you. To fix this problem set the StorageFlags of the X509Certificate2 with the following values:

return new X509Certificate2(
    certificateData, 
    securePassword,
    // MachineKeySet and Exportable are needed to be able to create Certificates inside
    // Azure Web Services
    X509KeyStorageFlags.MachineKeySet | 
    X509KeyStorageFlags.Exportable);

With these settings the Certificate is created successfully and my app is also working is Azure.

Happy Coding ;-)