|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe However, when using any kind of state management other than InProc (such as BackgroundSome browsing around returned a couple of good articles. The article Page tracking in ASP.NET offers a similar solution, though is geared around page tracking whereas my requirement was simply to find an alternative to the There's another excellent article called Preventing Multiple Logins in ASP.NET This is where I got the idea of using the application cache with a sliding expiry to trigger an event when the session ends. How it worksThe When the item expires and the callback method is called. The key, value and reason of the expired item is passed to this callback method. The key is the The callback method then checks that the item was removed as a result of it expiring, wraps the values into a Using the codeThe code consists of a First lets set up web.config <httpModules>
<add name="SessionEndModule" type="SessionTestWebApp.Components.SessionEndModule, SessionTestWebApp"/>
</httpModules>
<!-- Use the state server (rather than InProc), and set the timeout to 1 minute for easier testing-->
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="1" cookieless="false"/>
Then we need set up Global.asax protected void Application_Start(object sender, EventArgs e)
{
// In our sample application, we want to use the value of Session["UserEmail"] when our session ends
SessionEndModule.SessionObjectKey = "UserEmail";
// Wire up the static 'SessionEnd' event handler
SessionEndModule.SessionEnd += new SessionEndEventHandler(SessionTimoutModule_SessionEnd);
}
Then we need to create our event handler method for the private static void SessionTimoutModule_SessionEnd(object sender, SessionEndedEventArgs e)
{
Debug.WriteLine("SessionTimoutModule_SessionEnd : SessionId : " + e.SessionId);
// This will be the value in the session for the key specified in Application_Start
// In this demonstration, we've set this to 'UserEmail', so it will be the value of Session["UserEmail"]
object sessionObject = e.SessionObject;
string val = (sessionObject == null) ? "[null]" : sessionObject.ToString();
Debug.WriteLine("Returned value: " + val);
}
In this demo, lets also wire up the protected void Session_Start(object sender, EventArgs e)
{
Debug.WriteLine("Session started: " + Session.SessionID);
Session["UserId"] = new Random().Next(1, 100);
Session["UserEmail"] = new Random().Next(100, 1000).ToString() + "@domain.com";
Debug.WriteLine("UserId: " + Session["UserId"].ToString() + ", UserEmail: " +
Session["UserEmail"].ToString());
}
Testing the codeStart this project in debug mode and keep an eye on the output window. When the session starts, the session contents will populated with a random Returning more than one session valueThe For example: [Serializable]
public class SessionInfo
{
public string UserId;
public string UserName;
public string UserEmail;
}
SessionInfo sessionInfo = new SessionInfo();
sessionInfo.UserId = 10;
sessionInfo.UserName = "Bob Jones";
sessionInfo.UserEmail = "bobjones@company.com";
Session["SessionInfo"] = sessionInfo;
In Global.asax, you would then set the SessionObjectKey to 'SessionInfo': SessionEndModule.SessionObjectKey = "SessionInfo";
You can then access this in the SessionEnd event: private static void SessionTimoutModule_SessionEnd(object sender, SessionEndedEventArgs e)
{
Debug.WriteLine("SessionTimoutModule_SessionEnd : SessionId : " + e.SessionId);
SessionInfo sessionInfo = e.SessionObject as SessionInfo;
if (sessionObject == null)
{
Debug.WriteLine("Returned value is null");
}
else
{
Debug.WriteLine("Returned values - UserId: " + sessionInfo.UserId + ", UserName: " +
sessionInfo.UserName + ", UserEmail: " + sessionInfo.UserEmail);
}
}
Known limitationsAs the module hooks into the This code also won't work on scenerarios where you are using a server farm. In the article 'Preventing Multiple Logins in ASP.NET', Peter Bromberg discusses this problem in more depth and offers some ideas for working around this. HistoryVersion 1.0 - 02 November 2007
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||