Handling improper logout

G

Guest

I am working on this site in which users have to login first to access the
site pages, after a user performs a login a record for this user is inserted
in a table of online users. When a user chooses to logout, his record is
deleted from the onlineusers table,
I want this to happen too(deleting the user's record) when the user closes
the browser window without logging out. How can I handle this situation.

Thanks
 
G

Guest

You can place the login in the Global.asax file and add/delete user's records
when their session begins/ends:
void Session_OnStart()
{
// Create user's record
}
void Session_OnEnd()
{
// Delete user's record
}

Cheers,
Steve Goodyear
 
G

Guest

I've tried this but the session_end event never fires, I've placed some
tracing code in both events Session_Start & Session_End which writes to a
file, Session_Start works fine but nothing from Session_End, although the
session timesout normally and users are redirected to login page after
session timeout, but the Session_End event doesn't seem to fire at all.

Any idea?
 
G

Guest

Hi Jessy,

The Session_End will fire when each session expires. I made up a quick
sample to test below where in the Global.asax file I execute a stored
procedure I created in SQL Server which inserts a row into a table for each
event.

I also changed the Web.config file for sessions to timeout after only one
minute so I can see the effects right away. After a minute I queried SQL
Server to see the results.

Cheers,
Steve Goodyear


* Global.asax
....
protected void Application_Start(Object sender, EventArgs e)
{
RecordEvent("Application_Start");
}
protected void Session_Start(Object sender, EventArgs e)
{
RecordEvent("Session_Start");
}
protected void Session_End(Object sender, EventArgs e)
{
RecordEvent("Session_End");
}
private void RecordEvent(string evt)
{
cmd = new SqlCommand("EXEC RecordEvent @Event", db);
param = cmd.Parameters.Add("@Event", SqlDbType.VarChar, 50);
param.Value = evt;
db.Open();
cmd.ExecuteNonQuery();
db.Close();
}

* Web.config:
<sessionState mode="InProc" cookieless="false" timeout="1" />

* Query Results:
SELECT [Event], [RecordTime] FROM [dbo].[Event]
Event RecordTime
 
G

Guest

Thanks a lot Steve, your code worked with me.

As for mine the only thing I changed was instead of calling a function that
returns the current userid from the session varaible, i used the session
variable value directly in the session_end event and it worked that way, have
no idea why, but it's working now and that's fine for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top