Search code examples
c#asp.net-mvc-3application-variables

Share an object application-wide in ASP.NET MVC3


I have an ASP.NET MVC3 web application with C# and Razor.

Through a form in a View I get the Context.User.Identity.Name submitted to an action method in a Controller A.

I would like to have available this variable application-wide (between multiple controllers). Now I am just able to assign it to a global variable _UserName within the Controller A, but of course it is not available in Controller B.

Is it possible to do that?

Thanks

Francesco


Solution

  • If you are accessing this in any controller, you should use HttpContext.User.Identity in your controller methods - it will be available there. No need to store in the session.