Search code examples
c#asp.net-mvcx509certificate2

Loading X509Certificate2 crashing the ASP.NET MVC website


Simply I have X509Certificate2 certifacate which I want to load it in ASP.NET MVC Application.

simply here is the code

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        string path = HostingEnvironment.MapPath("~/Certificates.p12");
        // the path is correct, I logged it to a file and it was correct

        byte [] certBytes = File.ReadAllBytes(path);
        X509Certificate2 cert = new X509Certificate2(certBytes, "some password");
    }
}

and even this is not working

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        string path = HostingEnvironment.MapPath("~/Certificates.p12");
        // the path is correct, I logged it to a file and it was correct

        X509Certificate2 cert = new X509Certificate2(path, "some password");
    }
}

in the two previous cases, the code is working on my local machine, BUT when moving this code to our server, then the statement

X509Certificate2 cert = new X509Certificate2(/*whatever bytes or path*/, "some password");

is breaking, actually this statement get my IIS's Application pool to be stopped and I get Http 503 Service Unavailable error when I try to reach the website.

When I remove the previous statement, there is no 503 Error and the website is reachable.

I tried to move this statement to another class (putting it not in the startup of the website) but the result was the same.

EDIT for some very strange reason I can not log the exception which occur. so I do not know what is the exact problem of this

EDIT I just created a small console application, and I tried to read the same certificate file from it, and it worked perfectly both on my local machine and on the server, so I suspect the problem not from the certificate file and not from the server, but the combination of the ASP.NET MVC project with the certificate file is producing this problem

EDIT
Here is the Event log of the server

Application pool 'SmaresDev' is being automatically disabled due to a series of failures in the process(es) serving that application pool.

enter image description here


A process serving application pool 'SmaresDev' suffered a fatal communication error with the Windows Process Activation Service. The process id was '8520'. The data field contains the error number.

enter image description here


A fatal alert was generated and sent to the remote endpoint. This may result in termination of the connection. the TLS protocol defined fatal error code is 10. The windows SChannel error state is 1203.

enter image description here


Any Ideas.


Solution

  • I was experiencing something similar with a crash where no exceptions were being caught. I had the similar issues in the event viewer and also ran into the same thing with the console application working fine. I fixed it as the following

    X509Certificate2 Cert = new X509Certificate2(path, "some password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
    

    I'm not sure why it works, but it did for me, and hopefully does for you too.