title: Unhandled exception reading from X509CertificateStore
body: <p>I'm working with a really old project and moving it from on-prem to an azure VM running windows server 2016 and after the move we noticed an API endpoint returning Bad Gateway (502). I recreated the request using postman and noticed that I didn't even get a response back. After adding a bunch of logging i narrowed it down to the method below where it's suppose to read the security token from a certificate.
  It seems like an unhandled exception occurrs just before the code which iterates each certificate but I can't seem to catch the exception using UnhandledExceptionEventHandler.
  As far as I can see all the required certificates are in place. The one used here I even gave the user group "Everyone" full access just to just to eliminate that as a possible reason for the error
  In reality this method is full of logging for each line of code but I've removed it for readability. </p>
  
  <pre><code>        private static X509SecurityToken GetSecurityTokenBySimpleDisplayName(string simpleDisplayName)
          {
              if (string.IsNullOrEmpty(simpleDisplayName))
                  throw new ArgumentNullException("simpleDisplayName");
              try
              {
                  AppDomain currentDomain = AppDomain.CurrentDomain;
                  currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
                  using (X509CertificateStore store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore))
                  {
                      var isOpen = store.OpenRead();
  
                      int certIndex = -1;
                      /***** HERE IS WHERE IT CRASHES *****/
                      for (int i = 0; i &lt; store.Certificates.Count; i++)
                      {
                          if (store.Certificates[i].SimpleDisplayName.ToLower().Equals(simpleDisplayName.ToLower()))
                              certIndex = i;
                      }
  
                      if (certIndex &lt; 0)
                      {
                          throw new SecurityException("Certificate " + simpleDisplayName + " not found");
                      }
  
                      var token = new X509SecurityToken(store.Certificates[certIndex]);
                      return token;
                  }
              }
              catch(Exception ex)
              {
                  // Logging exception
                  return null;
              }
          }
  
          public static void MyHandler(object sender, UnhandledExceptionEventArgs args)
          {
              Exception e = (Exception)args.ExceptionObject;
              // Logging exception
  
          }
  </code></pre>
  
  <p>The application event log gives this error:</p>
  
  <ul>
  <li>Faulting application name: w3wp.exe, version: 10.0.14393.0, time stamp: 0x57899b8a</li>
  <li>Faulting module name: KERNELBASE.dll, version: 10.0.14393.3383, time stamp: 0x5ddcba29</li>
  <li>Exception code: 0xe0434352</li>
  <li>Fault offset: 0x0000000000034c48</li>
  <li>Faulting process id: 0x3528</li>
  <li>Faulting application start time: 0x01d5f20898415d08</li>
  <li>Faulting application path: c:\windows\system32\inetsrv\w3wp.exe</li>
  <li>Faulting module path: C:\windows\System32\KERNELBASE.dll</li>
  <li>Report Id: 64f16b87-a524-4e0e-9ab9-d8295ce7b29b</li>
  <li>Faulting package full name: </li>
  <li>Faulting package-relative application ID: </li>
  </ul>
  
  <p>How can I get a better idea of what's wrong?</p>