8

I have [Authorize] attribute on the HomeController, whenever I am trying to access it, it throws a NullReferenceException

This is really kind of weird, because I have used [Authorize] many times before and it works just fine. Only difference in this case is this Application is hosted on our own Web Server using Windows 7 & IIS 7.5

Here is Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +38
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +160
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +155
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 System.Web.Mvc.<>c_DisplayClassb.b_5() +62
System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +20
System.Web.Mvc.<>c_DisplayClasse.b_d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

Edit:

While looking into code of AuthorizeCore method, it seems that AuthorizeCore is throwing NullReferenceException because it gets a NULL HttpContextBase.

Could it be possible? Because everything else in application is working just fine, like accessing database, creating auth cookie etc.

Edit 2:

This happen only after publishing it to Web Server. While development, it works absolutely fine from Visual Studio.

Charandeep Singh
  • 942
  • 2
  • 7
  • 17
  • I got this exception because of an invalid cookie. If you experience this as well, try to open the developer tools in your browser, go to the "Resources" tab or similar, and delete all cookies related to the site. When you reload the page, it might work. :-) – Patrick Apr 16 '15 at 11:02

3 Answers3

4

Issue was even more worse HttpContext was not even available in Controller's and Razor views. So, I reinstall ASP.NET v4.0 using aspnet_regiis -ir. And then used ASP.NET 4.0 pool which was created during registration instead of using DefaultAppPool.

And it started working fine. It also solve my another issue of overriding <modules runAllManagedModulesForAllRequests="true"/> in my application web.config.

Charandeep Singh
  • 942
  • 2
  • 7
  • 17
1

In my case I was setting HttpContext.Current.User to null in one of the global.asax application events. When I let the User property unchanged the error disappeared.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

You probably need to make sure that the application pool your site is using has Managed Pipeline Mode = Classic.

Strillo
  • 2,952
  • 13
  • 15
  • Yes, I am using DefaultAppPool which is .NET 4, Integrated Pipeline and using Identity LocalSystem (to access SQL Server) – Charandeep Singh Aug 23 '11 at 10:50
  • Looks like the problem is that httpContext is null. Try to use the Classic .NET AppPool instead. See http://msdn.microsoft.com/en-us/library/bb515251.aspx#CLASSIC for some more details. – Strillo Aug 23 '11 at 11:36
  • After changing it to Classic App Pool, it shows that DirectoryListingModule Disabled Error, because its MVC. – Charandeep Singh Aug 23 '11 at 11:56
  • How did you fix this?! I'm having the same problem?! Really strange! – Tim Aug 26 '11 at 06:41
  • @Charandeep: I was having the same problem as you but finally solved it. What did it for me was that this line was missing in the web.config: Please see my thread on this http://stackoverflow.com/questions/7167251/mock-presence-of-authorize-attribute – Tim Aug 26 '11 at 12:51
  • Yes, I also have `` but in `applicationHost.config` using ``, because I was not able to override it in my application `web.config`. I also stumble upon this solution, but didn't work for me. Then I saw this http://stackoverflow.com/questions/2374957/asp-net-mvc-on-iis-7-5 and reinstall ASP.NET (this time I install it from **Framework64** directory because it is a 64bit machine) and it started working. – Charandeep Singh Aug 27 '11 at 10:32