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:
How can I get a better idea of what's wrong?
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