C
C#12mo ago
linqisnice

❔ Is there osme way to check if a user is active/online with aspnet core identity? (cookie)

Title is slightly incorrect: is there some way to check based on a users id if they are signed in* I understand that when you sign in, a cookie is created and sent to the browser and is then validated on each specified request. and then when you sign out, the cookie is invalidated... so there's no concept of bein "signed in"... in my situation, i need to check if x and y users are logged in inside a webhook request, and so there's no user request cycle here so i cant check the httpcontext if the request is coming from any given user. So... my thought was that I could maybe update a flag in the db when a user logs in or out, but when they close their browser, they would still be signed in but inactive. Is there soome "common" approach to this problem? how would a "last seen" function be implemented here? Would global action filter or middleware with a lastseen property be good enough?
47 Replies
linqisnice
linqisnice12mo ago
or signalr and check their connection
Reaper of Souls
Reaper of Souls12mo ago
There is no straight answer with this one. You would need to create a new table or column where you can store the user last logged in time and use some sort of websocket to update it after some few seconds or after 5 mins. There are few ways you can go about this but it's not fun
linqisnice
linqisnice12mo ago
ok so im on the right track then. i think there's a oslution with signalr. When the webhook request comes in, i can check the connection of the participants in the conversation. shouldnt that be enough? @mutlangi since the webhook is already responsible for calling sendasync on the hubcontext (i broadcast to the client only after the webhook confirms my message has been persisted in this case).
Reaper of Souls
Reaper of Souls12mo ago
That should work.
linqisnice
linqisnice12mo ago
but wouldnbt the problem the nbe that the connection is only established or broken if they are interacting with teh chat? technically they are still "online" if they navigate to other parts of the site
Reaper of Souls
Reaper of Souls12mo ago
With signalr or websockets. Connection doesn't need a user to interact with the application It will work on it's own.
linqisnice
linqisnice12mo ago
@mutlangiyeah but when should the connection first be established? or is that automatically done when they enter the site?
Reaper of Souls
Reaper of Souls12mo ago
You can do that. Look into how notifications works and how a chat app is able to know when a message is there without a user interacting with it. Dotnet does have libraries to assist you with that
linqisnice
linqisnice12mo ago
u got any libraries u can recommend? ill look into that @mutlangibut i feel like all i'd need to do is update teh flag in the hub OnDisconnected and OnConnected... and then simply fetch the status from teh db
Reaper of Souls
Reaper of Souls12mo ago
At the moment nop. I'm on mobile so I can't check any previous projects which implemented this kind of things. That can work. On in your client you need to check the hub.disconnrcted function. Maybe implement a function to update your server that client has disconnected
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
@TeBeClonewhat about using middleware and then caching user activity and a write-behind strategy of updating the database with the cache values every 5th/10th minute (or whatever threshhold i deem to be useful) or could youi redirect me to the docs i need to read on signalr? i'm reading here https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-7.0 currently, all I do basically is set user.IsLoggedIn = true in onconnectedasync and false in ondisconnectedasync. problem with this is obviously that if the user is on mobile and pc, and if he disconnects on one of them, it will look in the db as though he's offline
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
@TeBeCloneno I mean an alternate approach, separate from signalr. What im trying to do is track user activity, that's it. To see if they are "active" or "inactive" (not sure what that means, could be online, offline, whatever). Just some activity flag or some property indicating their last active status so the current approach is signalr and relying on connection but you could also use a middleware approach to track users activity, but it doesnt seem great to update the database on every request. So maybe cache and then use executeupdateasync every 10th minute or whatever.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
preferrably not use session
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
i've configured aspnet core identity with cookie configuration that's why
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
so no jwt
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
sure but when i say cookie i mean aspnet core identity so auth cookie
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
no but u said "you even mentioned cookie" that's teh cookie im talking about what im trying to do is, send an email to users when their conversation/DMs are updated when they are offline
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
it doesn't seem like a totally far-fetched thing to do
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
yes, sure, but how does that tell us whether the user is online or not? in my current setup, the cookie is remopved*when you explicitly sign out using the signinmanager. but when you close the browser, you are not signed out in my case
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
ok so... i communicate iwht twilio rest api, when their webhook tells me the message has been successfully received and persisted, i invoke icontexthub to push the message to all relevant clients here, i want to email the participants of the conversation if the yare not "online", if they are not signed in or are inactive, whatever
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
but the signalr approach i already have seems very simple and good enough then. since it doesnt have to be extremely accurate just good enough
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
can i not add OnValidatePrincipal to the application cookie events? and then add that users last activity to a cache
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
OnValidatePrincipal
Invoked to validate the principal. yes so this one i mentioned above? i dont think onsignedin is very useful, since we never know when they sign out unless they explicitly sign out?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
yes ok, but regarding db vs cache... should i use some write behind strategy or should i just stick to cache straight up? anyway thanks a lot was very sueful
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
the bult in imemorycache in this case because its not distributed?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
why? in case i scale up?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
sure
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
linqisnice
linqisnice12mo ago
man honestly at this point it seems like signalr could do the job very efficiently without much overhead and minimal code at least somewhat approximating an accurate flag on the user
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts