开发者

how can I know if user is logged in

开发者 https://www.devze.com 2023-03-05 14:51 出处:网络
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 web

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


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


I think what you are looking is

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消