gruber
gruber

Reputation: 29727

how can I know if user is logged in

Im using simple mechanizm in which after login in my database there is new row inserted with userId and expiresDate columns. Everything would be ok but how can I know if user leaves website, closes browser and so on ?

Secondly how can I make his sessionlonger if he is viewing different pages on the site. Should I all the time make updates on the database ?

what are common aptterns ?

It is im,portant for me because on the liveChat I need to know which users are online so that client can chat with them

Upvotes: 0

Views: 122

Answers (4)

AjayR
AjayR

Reputation: 4179

As David answered, there is no specific method to identify when user leaves the page. You need to use AJAX (in your chat system) or some other method to send server that user is still there in your server. When user dont respond for few interval you assume that he left your site and needs to update in the server.

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

I think what you are looking is

if(HttpContext.Current.User.Identity.IsAuthenticated)

Upvotes: 1

Bindas
Bindas

Reputation: 970

Use SQLServer State service to store the Session into the Database that will help you to get the details of all the online Users.

Upvotes: 1

Quentin
Quentin

Reputation: 943220

Everything would be ok but how can I know if user leaves website, closes browser and so on ?

There is no reliable way to know this. Expire sessions after a given period of inactivity.

Secondly how can I make his sessionlonger if he is viewing different pages on the site. Should I all the time make updates on the database ?

Yes. You should be using a standard session library for this, and that should take care of it for you.

It is im,portant for me because on the liveChat I need to know which users are online so that client can chat with them

If you're implementing a chat system, then use a heartbeat system. Have the client send, using JS, a "I'm still alive" message every minute or so.

Upvotes: 0

Related Questions