Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is needed in the HttpContext to allow FormsAuthentication.SignOut() to execute?

I am trying to write a unit test for our log out method. Among other things it FormsAuthentication.SignOut(). However, it throws a System.NullReferenceException.

I've created a mock; HttpContext (using Moq), but it is obviously missing something.

My mock context contains:

  • A mocked HttpRequestBase on Request
  • A mocked HttpResponseBase on Response
  • With a HttpCookieCollection on Request.Cookies and another on Response.Cookies
  • A mocked IPrincipal on User

I am aware I could go the wrapper route and inject an empty FormsAuth wrapper object in it's place, but I would really like to avoid the 3 additional files just to fix one line of code. That and I am still curious for an answer

So my question is "What is needed in the HttpContext to allow FormsAuthentication.SignOut() to execute."

like image 390
jeef3 Avatar asked Sep 15 '09 23:09

jeef3


1 Answers

The NullReferenceException in this case is actually being thrown by the call:

current.Request.Browser["supportsEmptyStringInCookieValue"]

You can test this assertion by calling:

HttpContext.Current.Request.Browser.SupportsEmptyStringInCookieValue

...which will also return the NullReferenceException. Contrary to the accepted answer, if you attempt to call:

CookielessHelperClass.UseCookieless(current, false, CookieMode)

...from the immediate window, this will return without error.

You can fix the exception like this:

HttpContext.Current.Request.Browser = new HttpBrowserCapabilities() { Capabilities = new Dictionary<string, string> { { "supportsEmptyStringInCookieValue", "false" } } };

...and the FormsAuthentication.SignOut() call will now succeed.

like image 194
Gracie Omoplata Avatar answered Oct 27 '22 05:10

Gracie Omoplata