500 internal server error using Firefox only

Viewed 151

I developed an ASP.NET web app that works just fine with all the browsers except Firefox

Introduction

My web app relies on a third party web app for authentication, my web app playing the role of the identity consumer and the third party web app playing the role of the identity provider

The authentication mechanism works as follows:

  1. My we app redirects the browser to the identity provider using a 302 message including a callback URL
  2. The identity provider authenticates the user and, upon successful authentication, redirects the browser to my callback URL using a 307 message including the identifier of the current user

In detail:

An unauthenticated user requests a page on my web app

My web app redirects the browser using the following response (irrelevant headers were omitted)

HTTP/1.1 302 Found
Location: https://identity-provider.example.com/Authenticate?Callback=https%3a%2f%2fmy-application.example.com%2fAuthenticationCallback.ashx

The identity provider, upon successful authentication, redirects the browser using the following response (once again irrelevant headers were omitted)

HTTP/1.1 307
Location: https://my-application.example.com/AuthenticationCallback.ashx?userId=Fabio

Hashes are actually included in both responses for mutual authentication of the two parties, but they are not relevant to this issue

Issue description

Using Firefox and Firefox only, my callback returns a 500 internal server error

So, I added some logging to the code of my callback

Trace.WriteLine("Callback invoked");
Trace.WriteLineIf(context == null, "ERROR: HttpContext is null!");
Trace.WriteLineIf(context.Request == null, "ERROR: HttpContext.Request is null!");
Trace.WriteLineIf(context.Request.Url == null, "ERROR: HttpContext.Request.Url is null!");
Trace.WriteLine($"The URL is \"{context.Request.Url}\"");
string userId = context.Request["userId"];
Trace.WriteLineIf(userId == null, "ERROR: The user ID is null!");
Trace.WriteLine($"The user ID is \"{userId}\"");
StringBuilder stringBuilder = new StringBuilder();
if (stringBuilder == null)
    Trace.WriteLine("ERROR: The string builder is null!");
else
    Trace.WriteLine("The string builder is not null");
stringBuilder.Append(userId);

The contents of the log file for each request is the following regardless of the browser being used

Callback invoked
The URL is "https://my-application.example.com/AuthenticationCallback.ashx?userId=Fabio"
The user ID is "Fabio"
The string builder is not null

So, no problems here

However, my callback returns a 500 internal server error using Firefox and Firefox only

This is the stack trace

[NullReferenceException: Object reference not set to an instance of an object.]
  com.fabioscagliola.MyApplication.AuthenticationCallback.ProcessRequest(HttpContext context) in C:\Data\MyApplication\Handlers\AuthenticationCallback.cs:113
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +541
  System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +74
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +92

Line 113 is the following

stringBuilder.Append(userId);

However, according to the contents of the log file, neither the stringBuilder nor the userId are ever null, not even when using Firefox

Thank you in advance for your support!

UPDATE

After Andrew Corrigan's comment, I added one line to the code of my callback, the last one in the following block, in order to ensure that the exception was actually thrown on line 113

Trace.WriteLine("Callback invoked");
Trace.WriteLineIf(context == null, "ERROR: HttpContext is null!");
Trace.WriteLineIf(context.Request == null, "ERROR: HttpContext.Request is null!");
Trace.WriteLineIf(context.Request.Url == null, "ERROR: HttpContext.Request.Url is null!");
Trace.WriteLine($"The URL is \"{context.Request.Url}\"");
string userId = context.Request["userId"];
Trace.WriteLineIf(userId == null, "ERROR: The user ID is null!");
Trace.WriteLine($"The user ID is \"{userId}\"");
StringBuilder stringBuilder = new StringBuilder();
if (stringBuilder == null)
    Trace.WriteLine("ERROR: The string builder is null!");
else
    Trace.WriteLine("The string builder is not null");
stringBuilder.Append(userId);
Trace.WriteLine("The user ID was correcly retrieved from the URL");

The contents of the log file for each request is now the following regardless of the browser being used

Callback invoked
The URL is "https://my-application.example.com/AuthenticationCallback.ashx?userId=Fabio"
The user ID is "Fabio"
The string builder is not null
The user ID was correcly retrieved from the URL

So, no problems here

However, my callback still returns a 500 internal server error using Firefox and Firefox only

This is the stack trace

[NullReferenceException: Object reference not set to an instance of an object.]
  com.fabioscagliola.MyApplication.AuthenticationCallback.ProcessRequest(HttpContext context) in C:\Data\MyApplication\Handlers\AuthenticationCallback.cs:114
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +541
  System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +74
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +92

Line 114 is the very new line of code I added

Trace.WriteLine("The user ID was correcly retrieved from the URL");

Now, nothing can ever be null here and, according to the contents of the log file, the execution of the callback continues to the subsequent instruction

Once again, thank you in advance for your support!

0 Answers
Related