59

I'm following this ASP.NET MVC tutorial from Microsoft:

My code is slightly different, where I'm trying to access HttpContext.Request.IsAuthenticated in the controller's constructor.

namespace SCE.Controllers.Application
{
    public abstract class ApplicationController : Controller
    {
        public ApplicationController()
        {
            bool usuario = HttpContext.Request.IsAuthenticated;
        }           
    }
}

The problem is that HttpContext is always null.

Is there a solution to this?

p.campbell
  • 91,713
  • 61
  • 243
  • 314
ozsenegal
  • 3,815
  • 12
  • 47
  • 63
  • Possible duplicate of [Null ControllerContext in my custom controller inheriting from BaseController](http://stackoverflow.com/questions/3236273/null-controllercontext-in-my-custom-controller-inheriting-from-basecontroller) – KyleMit Sep 02 '16 at 15:24

4 Answers4

110

instead of putting your HttpContext.Request.IsAuthenticated in Controller level you should put it in Controller Base class that will be inherited in all of your controller with an override method of OnActionExecuting() method.

In your Controller base you should have

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext ctx) {
        base.OnActionExecuting(ctx);
        ViewData["IsAuthenticated"] = HttpContext.Request.IsAuthenticated;
    }
}

and all your Controller should inherit the BaseController class

public class ApplicationController : BaseController

now you should get the ViewData["IsAuthenticated"] in your Master page.

Edit

With the link you have given, and relating to what you have done, your ApplicationController is a Page Controller, not a Base Controller. In the example, ApplicationController is a Base Controller that is inherited by the HomeController but what you have done is you are placing the Action method inside your base controller which is the ApplicationController so your Action Index method will not be invoked when you call any page (Index page) that is not from the ApplicationController.

Community
  • 1
  • 1
rob waminal
  • 16,673
  • 15
  • 47
  • 63
  • 3
    I had to change `Public` to `Protected` to make it work in my code, because of the error: _cannot change access modifiers when overriding 'protected' inherited member_ – jao Apr 27 '11 at 09:26
  • 1
    I would think you'd want to override `OnActionExecuting`, which occurs before the `Action` code runs, rather than `OnActionExecuted` which happens after -- so you have access to `ViewData` (or whatever) in your method – drzaus Jul 31 '14 at 19:44
  • 2
    You seem to be overriding `OnActionExecuting` but calling `base.OnActionExecuted` in the implementation. This looks like a typo to me. – spender Nov 28 '14 at 14:51
  • oh, yeah.. nice catch.. I've changed the overridden method to `OnActionExecuting` instead of `OnActionExecuted` and that part wasn't changed. Thanks – rob waminal Dec 15 '14 at 02:45
  • 1
    Should `OnActionExecuting(ActionExecutedContext ctx)` be `OnActionExecuting(ActionExecutingContext ctx)` ? – Martin Hansen Lennox Dec 22 '14 at 15:06
58

I would suggest you use:

 System.Web.HttpContext.Current.Request

Just remember System.Web.HttpContext.Current is threadstatic, but if you don't use additional thread the solution works.

Robert
  • 5,191
  • 43
  • 59
  • 113
Ghini Antonio
  • 2,171
  • 1
  • 19
  • 39
11

The Controller is instantiated significantly prior to the point where the Index action is invoked, and at the moment of construction HttpContext is indeed unavailable. What's wrong with referencing it in your controller method Index?

spender
  • 106,080
  • 28
  • 202
  • 324
  • 6
    only with DI in place controller constructors contain code and typically it is just one line _repository = repository – mare Aug 08 '10 at 17:07
10

The solution of this problem is to create an override method of Initialize by passing RequestContext object.

public class ChartsController : Controller
{
     bool isAuthed = false;
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);

        if (requestContext.HttpContext.User.Identity.IsAuthenticated)
        {
          isAuthed =true;
        }
    }
}