Handling improper logout

Discussion in 'ASP .Net' started by =?Utf-8?B?SmVzc3lFenp5?=, May 21, 2005.

  1. 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
    =?Utf-8?B?SmVzc3lFenp5?=, May 21, 2005
    #1
    1. Advertising

  2. 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
    =?Utf-8?B?U3RldmUgR29vZHllYXI=?=, May 21, 2005
    #2
    1. Advertising

  3. 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?
    =?Utf-8?B?SmVzc3lFenp5?=, May 21, 2005
    #3
  4. 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
    ---------------------------------------
    Application_Start 2005-05-21 13:34:52.133
    Session_Start 2005-05-21 13:34:52.623
    Session_End 2005-05-21 13:36:00.050
    (3 row(s) affected)
    =?Utf-8?B?U3RldmUgR29vZHllYXI=?=, May 21, 2005
    #4
  5. 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.

    "Steve Goodyear" wrote:

    > 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
    > ---------------------------------------
    > Application_Start 2005-05-21 13:34:52.133
    > Session_Start 2005-05-21 13:34:52.623
    > Session_End 2005-05-21 13:36:00.050
    > (3 row(s) affected)
    =?Utf-8?B?SmVzc3lFenp5?=, May 22, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dmE=?=

    RE: Logout status doesn't really logout?

    =?Utf-8?B?dmE=?=, Mar 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    577
    =?Utf-8?B?dmE=?=
    Mar 26, 2006
  2. =?Utf-8?B?RFdT?=

    RE: Logout status doesn't really logout?

    =?Utf-8?B?RFdT?=, Mar 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    571
    =?Utf-8?B?RFdT?=
    Mar 26, 2006
  3. =?Utf-8?B?dmE=?=

    Logout status doesn't really logout?

    =?Utf-8?B?dmE=?=, Mar 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    684
    =?Utf-8?B?dmE=?=
    Mar 26, 2006
  4. sp0
    Replies:
    10
    Views:
    2,426
    Buster
    Mar 4, 2004
  5. Dave
    Replies:
    3
    Views:
    318
    tom_usenet
    Aug 11, 2004
Loading...

Share This Page