The easiest way to update the profile information of an authenticated user profile for the current session

I have added some custom columns to the AspNetUsers table for Identity 2. I am using MVC5 from C #. There are several columns that contain data important to how the site handles the user, and there is a settings page that allows the user to change these settings.

Currently, if the settings are changed, the user must manually log out and log back in before the changes take effect, and this is not desirable.

What's the easiest way to update the user profile for their current session? If it requires a logout / login, how can this be coded using Identity 2? Several pages are not contained in AccountController and I am not sure how to access ApplicationUserManager and ApplicationSignInManager in AccountController. Doesn't seem like duplicating these objects outside of the AccountController, maybe that's acceptable?

+3


source to share


1 answer


If the only user who can change these settings, the user is, you can automatically perform SignOut

, and SignIn

for the user when he changes these settings. Basically all this is done using the methodSignInManager.SignInAsync

After the user has changed the settings, you can do something like:

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

      



Several pages are not contained in AccountController and I am not sure how to access ApplicationUserManager and ApplicationSignInManager in AccountController. Doesn't seem like duplicating these objects outside of the AccountController, maybe that's acceptable?

In my opinion, using in ApplicationUserManager

and ApplicationSignInManager

out is AccountController

not a good idea. If you are forced to do this, there may be a problem with the architecture of your application. If you are using an account controller, it must be responsible for all functions to handle user accounts (single responsibility principle)

+3


source







All Articles