How to set a global variable while the user is logged in?
I need to set a global variable, while the user is logged in. I need to be able to access this variable with both controllers and Views.
I am currently using the built-in MVC Membership pr开发者_如何学Pythonovider.
A user must not be able to edit this variable in any way.
I have tried using Session, but this expires at some point, while the user is still logged in.
Are there other alternatives?
You can use session cookies, encrypted values if security is a concern. Also take a look at the Application object which is global for the running application, not per user as the session object.
A technique I employed for my own needs was to create a BaseController
which inherits Controller
and then have my various controllers inherit that. In the BaseController
I put my "global" variable, which was set via the BaseController
constructor. All controller actions then could access this variable and make it available to other things (views, models) as necessary. It worked quite well in my situation.
精彩评论