Setting session variables for persistently logged in users
Hi all,
I need to make sure that logged in users always have a custom object stored in the current session.
It's obviously possible to create and fill the object at the moment a user logs in manually using a login control. But what if the user is already logged in persistently? What would be the best way to make sure that even they always get the object in the session?
In other words, what happens when a persistently logged in user opens a new session? Is there a method that's being invoked which I can use to implement a session object check/insert in?
Regards,
Erik
Hi Erik,
The ASP.NET Session_Start event that you can work with in Global.asax will fire once the current session starts, and will have a default timeout of 20 minutes. For more information you might check this article on MSDN. While the session is active, the Session_Start event will not get fired, so the only value you can store in that event is either on initial start of the session, or once it expires, and starts again. This looks like a convenient tool for the use case scenario you want to implement, so for example you can do:
if
(Request.QueryString[
"myQueryString"
] !=
null
)
Session.Add(
"UserID"
, Request.QueryString[
"myQueryString"
].ToString());
Response.Write(Session[
"UserID"
]);