ASP.NET Identity - Maintain login after updating user record

GigiSan

In a ASP.NET MVC web application, the admin may sometimes have the need to modify his user profile, thus altering his DB AspNetUsers record and triggering a SecurityStamp regeneration.

Modifying the SecurityStamp will eventually trigger the identity validation every 30 minutes server-side and cut the user's authentication, sending him back to login.

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = TimeSpan.FromMinutes(30), 
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });         

Is there a way to prevent this from happening but allowing me to keep the validation active? (something like forcing an identity "realignment" between server and client when saving the changes on the user profile)

Thanks in advance for every advice! - Gigi

RickL

You can re-SignIn the User after the changes have been made (this code assumes you have UserManager and SignInManager available as per the default Account controller and an async ActionResult):

        ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
        if (user != null)
        {
            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Identity user null after login

Updating user data - ASP.NET Identity

Asp.net identity - How to maintain logged in user role?

Pass user detail after Login Is successful From identity asp.net core 3.1

How do I get the user details straight after login using Identity on ASP.NET Core?

Cannot login on ASP.NET Identity 2 site after programmatic user creation

User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core

Add a Custom property to ASP.NET MVC Identity after user login

Updating user by UserManager.Update() in ASP.NET Identity 2

ASP.NET Identity record user registration and last logged on time

How to change user login credentials in identity asp.net mvc?

Overlap User Login in Two Projects with ASP.NET Identity

ASP.Net Core Identity login status lost after deploy

ASP.NET MVC5 Identity issue with User.Identity.GetUserId() during login process

How to save user last login date if he was logged in with external login provider using ASP .Net Identity?

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

Asp.net Identity keeps redirecting back to login page after successfull login

Redirection to User Profile After Login in ASP.NET MVC 5

Updating Identity in ASP.NET Core 2.0

Updating the database after editing the ASP.NET User using context in ASP.NET MVC

Where is the appropriate spot to seed and login a default user with ASP.NET MVC Identity 2.0?

ASP.NET MVC 5 Identity 2 Login redirect based on user role

Asp.net Identity 2.0 - redirect externally authenticated user based on role at login

How to disable account if user login fails continuously in Asp.Net Core identity

ASP.Net Identity - cannot login with password

ASP.Net MVC Alternative Login to identity

How to perform an automatic login after confirming the account with ASP.NET Identity

Adding Redirection immediately after Login in ASP.Net Core 2.1 using Identity Core

ASP.net Identity Login trigger on Click of Login

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive