aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.96k stars 868 forks source link

SignInManager.RefreshSignInAsync(inputuser) signs in current user instead of refreshing inputuser #1831

Closed Andrioden closed 6 years ago

Andrioden commented 6 years ago

Environment:

By my understanding RefreshSignInAsync can be used to refresh roles for an user after you have given it new roles. However, when I do, all it does is log the CURRENT_USER user in as the CHANGING_USER user.

Here is a simplified, but still bugging version of the code, with comments of debugging state

private readonly SignInManager<User> _signInManager;

public EditModel(SignInManager<User> signInManager)
{
    _signInManager = signInManager;
}

public async Task<IActionResult> OnPostAsync()
{
    //Debugging shows that the User.Identity.Name shows CURRENT_USER data.

    User changingUser = await _userManager.FindByIdAsync(InputUser.Id);
    // Debugging shows that changingUser shows CHANGING_USER data

    await _signInManager.RefreshSignInAsync(changingUser);
    return RedirectToPage("./Index");
}

On the redirect request I again check the debugging state to se which user data is present, and here the CHANGED_USER is now "logged in"

public async Task OnGetAsync()
{
    //Debugging shows that the User.Identity.Name now wrongly shows CHANGING_USER data.
}

The same happens with SignInManager.SignInAsync(dbUser, false, null); UserManager.UpdateSecurityStampAsync(dbUser);

Have I

  1. Missunderstood RefreshSignInAsync/SignInAsync?
  2. Or is this a straight up bug that happens for everyone?
  3. Or do I have to provide more contextual information because its a specific problem with my project. If so, what can influence something this?
HaoK commented 6 years ago

Sounds like you are misunderstanding how these work, Both SignIn/Refresh typically result in the user you pass in being the User.Identitiy.Name. Refresh sign in just carries over the authentication mode claim from the currently signed in user. And then signs in the user you pass in.

Andrioden commented 6 years ago

@HaoK : Hmm. Are you sure? Why do the RefreshSignInAsync() method take an ApplicationUser input parameter then? If it just uses the User.Identitiy.Name anyway?

HaoK commented 6 years ago

its the same thing as sign in basically, see https://github.com/aspnet/Identity/blob/dev/src/Identity/SignInManager.cs#L164

Andrioden commented 6 years ago

Hmm, I am sorry to be pushing this, but I am not convinced @HaoK : "The user to sign-in." and where the user derived princal.

HaoK commented 6 years ago

It doesn't, it uses the claims principal you pass it

Andrioden commented 6 years ago

Ok. I give up. You dont take your time to address

I have figured out a workaround for this lacking functionality, feel free to close the question.