How to log activity on Session Timeout or Window close - asp.net

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
}

Related

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.

On-Session-expire-event?

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.

how to create scheduler application for mail in asp.net?

my question is, I have to send mail at specific time daily, is there any way to do this in asp.net ?
give me appropriate suggestions.
Note : i don't want to run windows application or windows scheduler.
code which i used in global.asax
private static CacheItemRemovedCallback OnCacheRemove = null;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AddTask("Remoder", 5);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
public void Remoder()
{
HttpContext.Current.Response.Write("Hello Start");
}
Have a look at http://quartznet.sourceforge.net/
I think this technique from Jeff Himself is what you need:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
I tried the hack ... turning your application global.asax into a virtual scheduler. It was my choice, because my server admin department simply refuses (because they don't understand) to do it with windows, or a database job.
If I had my druthers though, I'd use a database "job", since sql-server can send mail directly. Not sure what db you're using, but if you have the necessary access, I'd recommend looking for a solution like that, instead of trying to fool your asp.net app.

Action after 10 minutes ASP.NET

In ASP.NET C# how to make a action after 10 minutes? It must be without the use of browser... Obviously an server side action...
You could set a timer in Global.asax to fire every 10 minutes:
private static Timer m_MailUpdateTimer;
protected void Application_Start(object sender, EventArgs e)
{
m_MailUpdateTimer = new Timer(MailUpdateTimer_Check, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
}
private static void MailUpdateTimer_Check(object state)
{
// Do something here.
}
protected void Application_End(object sender, EventArgs e)
{
if (m_MailUpdateTimer != null)
m_MailUpdateTimer.Dispose();
}
Of course, this will only fire if the web application is active, so if there is no usage for a while and IIS unloads it from memory, then the timer will not fire.
You may also want to consider using a Windows service or a scheduled job, which might be better suited for your needs.

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