I have a need to make a change to a database value at the moment that a user logs out. I can make this happen when the user clicks 'logout', but I need this to happen when a user times out as well. Is there some sort of listener or other such method I can use to achieve this?
Basically, due to SimpleMembership not supporting online user checking, I am forced to use a database field, a boolean, that records if a user has clicked login or logout, but obviously I cannot set this field to "false" if unless they click logout. Hence my need to detect automatic logouts, so to speak.
Any suggestions?
See: How to get notified of session end?
If you're sessions are in-process you can subscribe to the Session_End event. You will eventually (after the session timeout has expired) be notified that the session has ended.
ASP.NET has session states. So the one you should be interested is the Session_OnEnd event. But you can only use this inProc mode.
Refer to: http://msdn.microsoft.com/en-us/library/ms178583.aspx
Add this to Global.asax
public void Session_OnEnd()
{
//Perform your logic.
}
You will not be able to subscribe to any notification that lets you know when the session times out.
Instead, you will need to keep track (most likely in a database) of when the user performed their last action. Then have some sort of job that will periodically check to see those members that have not performed an action in any given time-frame and trigger the logout method.
Related
I would like to have the ability to check whenever I want that the user made an action related to his transactions, i.e. getting a refund outside of the app.
I am saving the SKTransaction's transactionIdentifier in the UserDefaults when he completes a transaction, however once this is done I have no idea how I can trigger a call to paymentQueue(queue, updatedTransactions) (or whatever function that could let me know the user's transactions state).
Thank you for your help
I have an application where a user can request something of another user. Using a model called Request it contains an order and the assigned user to which it applies.
What I would like to do is when the user receives a new request to notify them immediately, they should be able to accept or deny. Which is currently stored as a bool value in Request.accepted the default is nil.
This obviously requires back-end and front-end work. Have anybody done this or have experience with anything similar? Most ideal would be to display a bootstrap modal when a request appears, where they can accept or deny it.
It depends which version of rails you are using. More than one way is possible, also depends on the size of users and requests you are processing.
Add a after_save callback and call a mailers model
or add the request to a queue and run later a [1] ActiveJob
[1] http://edgeguides.rubyonrails.org/active_job_basics.html
I would need to record the value of an eVar only when the session ends.
Thank you,
Marco
a session ends when the session times out (as in, due to user inactivity), when the user leaves the site, or when the user closes the browser. There is no 100% reliable way to listen or mark any of these events. Your best bet is to populate the eVar on every page or activity and set attribution in interface to last value. But again, this will not reliably give you what you want.
I have a set of methods that clear cache data on log out, is there an easy way to trigger these functions when the session times out? I have updated my web config to have a time out of 1 minute for testing.
The issue i'm having is certain things displayed are meant to persist based on the user logged in, if the session times out and a new user logs in they will see the previous users information. This does not happen on log out because of the logic in place.
I don't need an answer but a pointer in the right direction on how to handle time out functions.
Add this to your global.asax:
public void Session_OnEnd()
{
//Find user's data using SessionId and remove it
}
What I did was simply ClearAllSessionKeys on log on instead of on session time out. This had the same effect and fixed my issue.
I am designing a feature to store the last logon date / time in an ASP.Net (MVC) application.
My first instinct was to store the value in the database against the user's profile record and update the value to the current date/time on successful login. Of course, as soon as I record that value, all pages will display the date and time of this session's successful logon.
Plan B: A field to record the previous session and one to record this session. On logon, save this session's date/time to the "current" field and move the value previously found there into the "previous" field (obviously). It is this field that provides my "last logged in on" value.
Is this the best approach or can it be done more elegantly?
Another approach is to, when logging in, read the last login date/time from the user record and save it into the session or a session cookie. Then update the user record with the current date/time. Then on your pages read the value stored in the session/cookie.
The old time will be removed when the session expires which is usually when a user needs to re-login anyway. It also has the advantage of speed and caching as it is reading from the session/cookie.
But it depends on your setup and app whether this is possible for you.
UPDATE
Just to be clear... The current date/time is persisted to the database user table every time the user logs in. But before the date/time is written to the user table, the existing value is read and saved to the session or cookie. You then update the date/time value in the user table with the current timestamp.
If your authentication ticket lasts longer than the session then use the cookie method and set the expiry of the cookie to the same expiry of the authentication ticket.
There are a couple other ways you could do this...
Instead of having a column on the user's record, you could have a separate table that logs everyone's logins. This would also grant you the ability to have a "show last 5 logins" feature if seeing the last login date is important or to keep statistics on login data for reporting later on. (This would build up data over time and would probably need some sort of cleanup routine or schedule script.)
The global.asax has a Session_End event (or something like that). When the user first logs in that value could be stored in a session variable, then when Session_End fires it's written to the database. This method would probably end up causing more oddities than it's worth, as you'd always wonder what happens if the Session_End doesn't fire, or if the user re-logins before Session_End fired for the first login.
It's been a while, but there was a sort of middleware that asp.net let you inherit from a base class and implement code that handles pre or post session begin/end. I haven't done ASP in a while so I'm rather hazy on this.
How about creating a custom serializable class that implements this functionality?
Such a class could simply be serialized/deserialized as a blob to the user's profile. Unless you need to be able to query on the last logon date, this could be a good solution.
My first thought was to simply use a Stack<T> to implement this, but although it is serializable, it provides no hooks that will automatically evict old values.