title: Loading X509Certificate2 crashing the ASP.NET MVC website
body: <p>Simply I have <code>X509Certificate2</code> certifacate which I want to load it in ASP.NET MVC Application. <br/></p>
  
  <p>simply here is the code </p>
  
  <pre><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");
      }
  }
  </code></pre>
  
  <p>and even this is not working</p>
  
  <pre><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
  
          X509Certificate2 cert = new X509Certificate2(path, "some password");
      }
  }
  </code></pre>
  
  <p>in the two previous cases, the code is working on my local machine, BUT when moving this code to our server, then the statement</p>
  
  <pre><code>X509Certificate2 cert = new X509Certificate2(/*whatever bytes or path*/, "some password");
  </code></pre>
  
  <p>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.</p>
  
  <p>When I remove the previous statement, there is no 503 Error and the website is reachable.</p>
  
  <p>I tried to move this statement to another class (putting it not in the startup of the website) but the result was the same.</p>
  
  <p><strong>EDIT</strong> 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</p>
  
  <p><strong>EDIT</strong> 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 </p>
  
  <p><strong>EDIT</strong> <br/>
  Here is the Event log of the server</p>
  
  <blockquote>
    <p>Application pool 'SmaresDev' is being automatically disabled due to a series of failures in the process(es) serving that application pool.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/SLJ4b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SLJ4b.png" alt="enter image description here"></a></p>
  
  <hr/>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/4J4Eq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4J4Eq.png" alt="enter image description here"></a></p>
  
  <hr/>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/V3gsb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V3gsb.png" alt="enter image description here"></a></p>
  
  <p><hr/>
  Any Ideas.</p>