locked
How to know that user currently logged in or logged out from session database RRS feed

  • Question

  • User900546029 posted

    I have application that uses sql server to store session data on the database. 

    Now i want to know that if user runs application that the session is created and stored in the database.

     than how to know that user is logged in or logged out from application. ??? 

    thanks 

    prashant Chidhade 

    Tuesday, September 20, 2011 2:15 AM

Answers

  • User-1856974186 posted

    Actually, thinking about it, you can do it without a module. You could, for example, do something like:

    1. In global.asax, get the session id and user ID and write them to a custom table. Do this in the Application_AuthenticateRequest event, which happens after the session has been created and the user authenticated.

     - The table only needs two columns (UserID(uniqueidentifier) and SessionID (nvarchar(88)).

     - When you write to the table, make sure there isn't already a matching entry; the AuthenticateRequest happens for every request, so you don't want to write the data everytime.

    2. In global.asax, in the Session_End event, get the session ID and user ID and delete the entry from the custom table. This clears it up and keeps the table clean.

    You now have a table that matches the session to the user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2011 10:22 AM

All replies

  • User-248200860 posted

    Hi friend,

    When User Logged into the system maintain varaible in seesion filed 

    when u want check the user is logged or not 

    if seesion["logged"] value is true user is loged in 

    other wise user is logged out and Session time is completed 

    Thanks...

    Tuesday, September 20, 2011 2:27 AM
  • User900546029 posted

    but ASPStateTempSessions does not maintain any column for this login logout process...??? my goal is to print session id of user logged in. than when user login from different browser than new entry will be made to database than how to know that same user has come and inserted username and password and display all session id of same user logged in from diffrent locations ???

    Tuesday, September 20, 2011 2:36 AM
  • User-1856974186 posted

    The membership API can help with some of this. For example, the MembershipUser class has IsOnline, LastLoginDate and LastActivityDate properties which could be useful. You can use this like so:

      MembershipUser usr = Membership.GetUser(); // gets the current user
      MembershipUser usr = Membership.GetUser("Username");  // gets the name user

      if (usr.IsOnline)

    The only way to access session details would be to directly query the session table.

    One problem you have though is that the session has a timeout, so closing a browser and opening another one would create a new session; eg I hit the close button by mistake, so I restart the browser but now I have a new session.

    Tuesday, September 20, 2011 3:56 AM
  • User900546029 posted

    yes thanks thats what i wanted...thanks for your reply....

    Tuesday, September 20, 2011 3:59 AM
  • User900546029 posted

    hey dave 

    i have created the membership database and data are storing quite nicely. but now how can i map user with his session id in session tables.??

    i searched membership tables but it does not store any data regarding session. so how can i know that user is logged in from particular how many session ids.

    Tuesday, September 20, 2011 7:23 AM
  • User-1856974186 posted

    If you're using in-process session state (the default) it doesn't store any session details in the database. If you use SqlServer mode it will create additional tables when you configure the session state. For that, see http://support.microsoft.com/kb/317604.

    Tuesday, September 20, 2011 9:41 AM
  • User900546029 posted

     i am using sql server mode. and i have configured it and session is storing but how to map session tables and membership tables

    prashant

    Wednesday, September 21, 2011 1:28 AM
  • User-1856974186 posted

    Well, you can't really. You can only match the existing session because that's the only one you know about. You can query the session state table, but you don't have access to session IDs from other sessions and there's no map to the users. This is done by default for security reasons.

    The only option here would be to write an HttpModule that sits after the Session & Membership ones and does the mapping.

    Wednesday, September 21, 2011 3:20 AM
  • User900546029 posted

    so how can i do it with the help of http module...can you provide me some tutorial...??

    Wednesday, September 21, 2011 4:12 AM
  • User-1856974186 posted

    Actually, thinking about it, you can do it without a module. You could, for example, do something like:

    1. In global.asax, get the session id and user ID and write them to a custom table. Do this in the Application_AuthenticateRequest event, which happens after the session has been created and the user authenticated.

     - The table only needs two columns (UserID(uniqueidentifier) and SessionID (nvarchar(88)).

     - When you write to the table, make sure there isn't already a matching entry; the AuthenticateRequest happens for every request, so you don't want to write the data everytime.

    2. In global.asax, in the Session_End event, get the session ID and user ID and delete the entry from the custom table. This clears it up and keeps the table clean.

    You now have a table that matches the session to the user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2011 10:22 AM
  • User900546029 posted

    great Dave 

    thanks, thanks a lot for this....

    Friday, September 23, 2011 1:13 AM