On-Session-expire-event? - asp.net

I'm programming an MVC3 application. Now I hava to call a script if the users session expire.
Is there something like a event on-session-expire, that get fired when the user session expired?
Thanks a lot!

In your Global.asax
you can create a
protected void Session_End(object sender, EventArgs e) { }
method which should be called when a session ends.

Related

Initialize variables before having anyone enter the web app

How can I initialize variables before my Web Application starts? I want to have, for example an Application type, to count number of logged in users in my website (it's an example, I have more variables that need initialization). How can I perform that?
It could be very useful if there was some event that would be called at the start of the Web App. I thought that I could check in each Page_Load event if a certain Session called, e.g Session["Started"] is null, and if it, redirect to an .aspx page for initialization. Or, even better, have a class called, e.g MyPage which inherits from System.Web.UI.Page and have her constructor get a function as an argument, which will be called after the initialization of the base class Page_Load.
Maybe I'm just bothering, is there any built-in event called that I can overwrite to initialize everything I want in the beginning?
You can use the Global.asax file for this. In particular you can use the Application_Start, Session_Start, Session_End events.
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
}
Here for more informations.

How to log activity on Session Timeout or Window close

In my application, we want to log some activity and messages to the DB when the window is closed by the user or when the session timesout. Is there any tested code to do this? Is writing this code in Session_End method of Global.asax.cs the right way?
You may be able to do something like this for the session (note that there is no guaranteed way to check for a browser closing). This approach will only work if you are using inProc for your session.
In your global.asax.cs
protected void Session_Start(object sender, EventArgs e)
{
//a value must be present to call Session_End so we pre-seed it
Session["SeededValueToEnableSession_End"] = true;
}
protected void Session_End(object sender, EventArgs e)
{
//handle session ending
}

Adding an event handler to a changing session value in asp.net c#

Can I add an event handler to a session value in asp.net with c#?
I'm planning to update the database when user logs in and logs out and I control login and logout with session values, but the session may end without a user click, like timeout, so I'd like to add an event handler to "isloggedin" session value.
you need to handle this in global.asax.
protected void Session_Start(Object sender, EventArgs e)
{
//your code
}
protected void Session_End(Object sender, EventArgs e)
{
//your code
}
You can implement the Session_End method in the global.asax. There, you will retrieve what information you need from the session, and do what you need to (like setting logout flag in database for the user):
Here's an example of what I do, though you can tailor it to your needs:
void Session_End(object sender, EventArgs e)
{
SessionPlayerContext context = (SessionPlayerContext)this.Session[Constants.SessionKeys.UserContext];
if (context != null)
PlayerManager.SetPlayerOnlineStatus(context.PlayerID, false);
}
The key is that I'm getting the user object that I previously stored in the session (when user logged in), and if it exists, I then flag the user as being logged out in the database (via PlayerManager)
Not only should you check it in the Session_End, but you also should check it wherever user would physically log out.
As for setting the user as being logged in, you will handle that when user physically logs in.

Regarding Session_End and Application_End methods in asp.net

I'm trying to implement a class (lets call it CustomerConnection class) which implements System.Web.HttpApplication interface. I tried to implement two events session_end and application_end events in this class in this way:
public class CustomerConnection: System.Web.HttpApplication
{
protected void Session_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
SqlConnection connection = context.Items[Database_Con] as SqlConnection;
connection.close();
}
protected void Application_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//someotherEvent
}
But it seems these events are not getting executed neither for the application end or session log out.
How should i make these events get´executed ? Is it like i have to notify to certain other event?
I'm just creating a new object of customer class. is it enough to get these events executed or my approach is fundamentally wrong?
Please help me out
Thanks in anticipation
You need this in global.asax:
<%# Application Language="C#" Inherits="CustomerConnection"%>
Caveat, I've never tried this, I found it here: http://www.codedigest.com/Articles/ASPNET/34_Adding_CodeBehind_for_Gloabalasaz_x_file_in_aspnet_20.aspx
The object that you have created isn't connected to anything, so it can't receive any events from the application. You have to put the event handlers in the object that is used for the web application, i.e. in the Global.asax.cs file.
Besides, there is no Session_End event, it's Session_OnEnd.

How to find information about all sessions for a web-app/site

I am trying to create an admin screen that will give me details about all open sessions in an application/site. I would also like to know how many session objects are active for each of them
Session object gives me info about my current session. How do i find info about all open sessions. How many sessions are active, etc.
Thanks,
SK
Assuming you want to do this in your ASP.Net code, and not by using a web server tool, you can increment a counter in an Application (or Cache) variable on Session_Start, and decrement it on Session_End in Global.asax.
If you want to know more than the count of active users, you can accumulate user information in a collection there -- a List<T> of User objects, perhaps.
Here's some code to get you started with this approach:
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) + 1;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) - 1;
Application.UnLock();
}

Resources