rwkiii
rwkiii

Reputation: 5846

Easiest way to refresh authenticated user's profile data for current session

I've added a couple of custom columns to AspNetUsers table for Identity 2. I'm using MVC5 with C#. There are a couple of columns that contain data that is important to how the site handles the user and there is a settings page which allows the user to change these settings.

Currently, if settings are changed the user must manually log out and log back in before the changes will take effect and this is proving to be undesirable.

What is the most straightforward way of refreshing a user's profile for their current session? If this requires logout/login to take effect how might this be coded using Identity 2? A couple of pages are not contained in the AccountController and I'm not sure how to access the ApplicationUserManager and ApplicationSignInManager in the AccountController. It doesn't seem right to duplicate these objects outside of the AccountController, maybe this is acceptable?

Upvotes: 3

Views: 1715

Answers (1)

Alex Art.
Alex Art.

Reputation: 8781

If the only person who can change those settings is a user itself you can automatically perform SignOut and SignIn for this user when he modifies those settings. Basically all this done by SignInManager.SignInAsync method

After user modifies it's 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);
}

A couple of pages are not contained in the AccountController and I'm not sure how to access the ApplicationUserManager and ApplicationSignInManager in the AccountController. It doesn't seem right to duplicate these objects outside of the AccountController, maybe this is acceptable?

In my opinion using ApplicationUserManager and ApplicationSignInManager outside of an AccountController is not a good idea. If you are forced to do so probably there is a problem with your application architecture.If you are using an Account controller, it should be responsible for all the functionality for user account handling (Single Responsibility Principle)

Upvotes: 3

Related Questions