Unhandled exception reading from X509CertificateStore

0

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.

        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 < store.Certificates.Count; i++)
                    {
                        if (store.Certificates[i].SimpleDisplayName.ToLower().Equals(simpleDisplayName.ToLower()))
                            certIndex = i;
                    }

                    if (certIndex < 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

        }

The application event log gives this error:

  • Faulting application name: w3wp.exe, version: 10.0.14393.0, time stamp: 0x57899b8a
  • Faulting module name: KERNELBASE.dll, version: 10.0.14393.3383, time stamp: 0x5ddcba29
  • Exception code: 0xe0434352
  • Fault offset: 0x0000000000034c48
  • Faulting process id: 0x3528
  • Faulting application start time: 0x01d5f20898415d08
  • Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
  • Faulting module path: C:\windows\System32\KERNELBASE.dll
  • Report Id: 64f16b87-a524-4e0e-9ab9-d8295ce7b29b
  • Faulting package full name:
  • Faulting package-relative application ID:

How can I get a better idea of what's wrong?

c#
x509certificate

1 Answer

0

Found the answer. The application pool needed to be set to "Enable 32-bit application" once that was in place it started working again


User contributions licensed under CC BY-SA 3.0