Asp – MS MVC – Can I somehow globally fill in strongly typed ViewData/Model for all the Views

asp.net-mvcmodelviewdata

I'd like to supply instance of logged User (or null if nobody logged yet) for all views in my MVC application. I've already succesfully implemented my own ControllerActionInvoker class which overrides InvokeAction and supplies logged user instance in ViewData (controllerContext.Controller.ViewData["LoggedUser"] = xxx). The problem is that I'd like to use the strongly typed Model for passing the logged user instance in whole application. I was thinking about having ApplicationViewDataBase class which could be base class for all my strongly typed ViewData classes and which would provide logged user instance also. I could than easily access logged user instance in all my Views.

Is it possible to fill in the strongly typed globally like I achieved in ControllerActionInvoker.InvokeAction override? Or is it better to somehow supply my User instance in Page.User? I'd find probably more slick to use Page.User, but also didn't find any solution how to inject my User instance…

Best Answer

Maybe better way to put User into the HttpContext? In global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs e)