I know similar questions have been posed before, but I could not find a duplicate that handles ASP.NET MVC (and the controller aspect) specifically.
My question is as follows:
In a MVC model, my understanding is that the controller should handle the use of HttpContext to determine who is logged on, if any. This is so the controller can present this information to the view, so that the view itself does not have to perform these look-ups.
Is there any defacto standard on how to do it?
My current setup is as follows [simplified]
I have a BaseController
, which all my other controllers inherit.
In BaseController
, I perform the following override:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
ViewData["IsUserLoggedIn"] = IsUserLoggedIn; // bool property checking HttpContext.User.Identity.IsAuthenticated;
ViewData["CurrentUser"] = CurrentUser; // Property returning logged in user, or null.
}
Then, naturally, in my Views I can check the ViewData values.
How do you do it? I'd like to know if something is wrong with my setup, and if so, what's wrong? I'm not 100% comfortable with my solution, - mainly because I'm not too familiar with the cycle, and I'm uncertain whether I've placed my code at the correct place.
I'm aware there might not be "one answer binds them all" here, I'm accepting the answer that offers most insight.