save membership in session variable when authenticated - c#-3.0

I would like to bound a sqlconnection paramater to a session variable holding the current logged in user, is there an glboal event that triggers whenever the membership user is set/changed so I can set at the event the session variable holding the current logged in user?
Thanks

Related

Firebase logout clients when email or password changed

I have a functionality in my app where the users can change their password or update their email address.
According to this document https://firebase.google.com/docs/auth/admin/manage-sessions i would assume that when the same user is logged in on another device at the same time that the refresh token should be revoced when the password changes and the user should be logged out.
It seems that this does not happen in my case. I use these two listeners to detect the changes:
addStateDidChangeListener
addIDTokenDidChangeListener
They are normally working when I login/logout via the Firebase login and logout functions but they don't get called when I change the password on another device.
Is there any way to get notified when a user change the password on another device?
Firebase Authentication uses ID tokens to maintain the authentication state on the client, and these tokens are valid for one hour. Only once the SDK needs to refresh the token (which it automatically does about 5 minutes before the token expires) is it guaranteed to detect the change, so that may take up to an hour.
If you want to force refreshing of the token before it expires, you can call reload on the User object.

Keycloak - User Session Note Expires Too Early

I have a custom required action that attaches a custom attribute to the user's session by setting a session note.
requiredActionContext.getAuthenticationSession().setUserSessionNote("key", "value");
The problem is that this note disappears (gets deleted from the session) after about a minute (I believe that it's the expiry of the access token) and it means that every minute I'm required to fill the required action form again.
The session itself doesn't expired because I'm not prompted to enter my username/password again. Only the session notes expire.
Is there a way to attach custom attributes to the user's session that would last as long as the actual user session (will last as long as I'm not required to re-enter credentials)?
My problem was that I was setting the required action (addRequiredAction) in the custom user storage provider. It seems like calling addRequiredAction clears the session notes.
I fixed it by creating a custom authenticator that would run after the username/password form and setting the required action there.

Keycloak token doesn't include mapped user session note on first login

I have created a custom IDP and whenever I login I need to set a User Session Note which is mapped in Keycloak and then added to the access token. The problem I am facing is that this User Session Note is not saved the first time a user logs in. It works the second time, and third and so on, but on the first login the user does not simply get the key/value added to the token.
The login succeeds every time, however it is missing the user session note that I wanted to be mapped on the token.
I'm not sure what the problem is. The IDP is set up to have a custom "First Login Flow" which has Create User If Unique (alternative) and Automatically Set Existing User (alternative). Maybe there is something here that I need to change so that it also includes User Session Notes on token for the first login?
I found that you need to implement the importNewUser() method in your extension of AbstractIdentityProviderMapper and set your notes on the authenticationSessionModel there.
Inside of the authenticated method, where preprocessFederatedIdentity() is called, Keycloak checks if federatedUser == null. At this point, you are in the state of preprocessing the federated user; it does not yet exist. Keycloak then calls resetFlow() within this if block where it then clears auth notes from the authenticationSessionModel.
Hopefully someone finds a more delicate solution than I did, but I managed to solve this by a little hack.
I noticed that in the provider I could override the method "preprocessFederatedIdentity" and when listing the User Session Notes there I saw that they do in fact exist on the session. However, when reaching the method "authenticationFinished" (also overridden) they have been cleared from the session (the clearing seems to happen right before method "updateBrokeredUser" is reached). This odd behaviour only happens on the first login.
What I then noticed is that User Attributes stored on the context however are not cleared. They persist, and this was key to my solution. Then what I did was simply to loop through all of the User Session Notes in the method "preprocessFederatedIdentity" and store them as User Attributes (with the key prefixed with "UserSessionNote:"), and in the latter step "authenticationFinished" I add them to User Session Notes again. I also stored all of the keys of the User Session Notes in a User Attribute called "UserSessionNotesKeys" as a comma separated string so that I could simply split that string and loop through the attributes in the "authenticationFinished" method and then add them to User Session Notes.
This basically solved my issue in a generic way, and also made sure it would work if future User Session Notes were added.

Play Framework - Disconnect an user

I'm doing a Scala - Play application and I want to disconnect an user when an admin change his right. For exemple, is an user is logged and an admin upgrade his account to the admin type, I want disconnect this user.How can I do that ?
If you stored the userId in the Session you will need to add the rights of the user in the Session.
So that when the user connects, you can check his rights from the session to the ones in your database. If they don't match you can redirect the user to the login page.
just to cover all the bases since the answer here depends specifically on how your application determines if a user is logged in:
if user auth is done with a token generated by a secret that is stored on your user model and checked for validity on every request, you can generate a new secret for the user and all existing tokens will become invalid.

Automatically signing out of other logged in sessions

In Facebook when you change your password it has the options to automatically sign you out of any other devices logged into the same account. How does this work?
When creating the session, a session key is stored in the database alongside the cookie. This means cookies are validated server-side, with data from the database. Once logged in a user can view the session data retrieved from the database. This gives the user the opportunity to sign out of any other device because once the database entry is removed, the cookie with the same key won't validate anymore.