Recently in a web application, we needed to initialize X509Certificate2. So we used below code block for that:

var certificate = new X509Certificate2(FullPathOfKey, KeySecret,
X509KeyStorageFlags.Exportable);

And above code just worked fine on our development machine.
Note: Development machine is with Windows 7, IIS 7.5 and .Net 4.5

But as soon as we moved the code to staging, which is having Windows server 2012, IIS 8 and .Net 4.5; It stopped working. We checked the log file, but no exception!

Then we started tracing and found it's just crashing in above mentioned line, without throwing any exception. And moreover it's crashing app pool and the whole site was not responding for couple of minutes, without leaving any clue why it's happening.

After doing some search, we found the solution. Below is the code block, that worked for us:

var certificate = new X509Certificate2(FullPathOfKey, KeySecret, 
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);


Hope, it helps you in similar situation!

Discussion
Nine - 8 =
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Mritunjay Kumar
Works at Mindfire Solutions

I mostly work with C#, ASP.NET, MVC, WCF, Web API, Entity FrameWork, MS Sql.

More under this category...
Convert data table to generic list
Extension method to convert a delimited string to list of generic primitive type
Convert generic list to data table
Generate random string from set of fixed chars
Google cloud storage API - Rename a file
Google cloud storage API - upload file with progress bar
Google cloud storage API - download file with progress bar
All under this category...