What could cause `UserManager` to return the wrong user?

前端 未结 1 379
遥遥无期
遥遥无期 2020-12-10 23:37

Something rather scary is happening on my ASP.NET Core 2.1.0 MVC site. While I was browsing, all of a sudden it shows I am logged in as a different user (who also happens to

1条回答
  •  囚心锁ツ
    2020-12-11 00:19

    Good news! I was causing this myself (I believe, help me figure this out by reading the details below). You can thus rest assured that unless you are making the same mistake as I am, the ASP MVC authentication mechanism is not to blame here (at least, that is my current understanding).

    I will document what exactly I did wrong, and how to replicate, since others might possibly make the same mistake.

    In short: I called SignInManager.RefreshSignInAsync(TUser) with the 'wrong' user (the one I ended up being logged in as), causing me to be logged in as that user.

    Why did I do this? In my specific use case, I wanted to hand out a claim to another user based on the action of the currently logged in user. I therefore called:

    await _userManager.AddClaimAsync( userToGiveClaim, newClaim );
    await _signInManager.RefreshSignInAsync( userToGiveClaim );
    

    I called RefreshSignInAsync since I wanted to prevent the user who had been given the claim from having to log out and in for the new claim to go into effect. From the RefreshSignInAsync documentation I got the impression this should work:

    Regenerates the user's application cookie, whilst preserving the existing AuthenticationProperties like rememberMe, as an asynchronous operation.

    Parameters user The user whose sign-in cookie should be refreshed.

    I'm still not entirely clear why the user that is currently logged in when this call is triggered gets the identity of the user passed to this call. That is still not how I understand the documentation (I filed this as a bug report), but since this is reproducible I am now more inclined to believe I simply misunderstood the documentation.

    0 讨论(0)
提交回复
热议问题