How “Session_Start” in Global.Asax works ?. MVC

2019-08-05 17:19发布

问题:

I'am trying uderstand how "Session_Start" in Global.Asax works. Let's say I've got few pages on my website. For example: I'am entering on "Page A", then function "Session_Start" is firing. Then I'am going to "Page B", and what than ?. Function will firing again, or not ?. I'am trying to write code which will check that cookie "User" exist, and if exist, code will return Session["userName"] variable, but I'am not sure is "Session_Start" good place for that..

回答1:

If you want to make session unlimited, you can use cookies. Which I think you are using, you mentioned that in your original query.

Session_End is fired when user logs out, closes browser to end the session or session times out.

You can keep the cookie on user's browser. And check the cookie in Session_Start for valid values. And resume the session for the user.

You can also increase the session timeout from server configuration.



回答2:

No it will not fire again, for second page. Session_Start fires the first time when a user's session is started

You can use this to check user cookie and save the result to session variable to access in further pages.