title: ASP.NET Core Identity impersonate specific user
body: <p>Let's say I have the following roles: </p>
  
  <ol>
  <li><p>Admin</p></li>
  <li><p>User</p></li>
  </ol>
  
  <p>I want Admin role to impersonate specific user account with User role, but without knowing that specific user account's password.</p>
  
  <p>Admin should be able to impersonate any user from the application and be able to browse the application as the user himself.
  <a href="http://tech.trailmax.info/2014/06/user-impersonation-with-asp-net-identity-2/" rel="noreferrer">I found a  link</a> where this is actually implemented in ASP.NET MVC 4.6, but having a little headaches while converting this to Core version.</p>
  
  <p>Mostly because of the last line of code in the link </p>
  
  <pre><code>authenticationManager.SignIn(new AuthenticationProperties() 
  { IsPersistent = false }, impersonatedIdentity);
  </code></pre>
  
  <p>where <code>SignIn</code> parameter in .NET Core does not allow <code>IdentityResult</code> class (impersonatedIdentity) to be passed anymore. It can now only take <code>ClaimsPrincipal</code>. </p>
  
  <p>So what I ended up doing was this, </p>
  
  <pre><code>public async Task&lt;IActionResult&gt; ImpersonateUserAsync(string userName)
      { 
          var impersonatedUser = await _userManager.FindByNameAsync(userName);         
  
          var claims = new List&lt;Claim&gt; {
              new Claim(ClaimTypes.Name, impersonatedUser.FirstName, ClaimValueTypes.String),
              new Claim(ClaimTypes.Surname, impersonatedUser.LastName, ClaimValueTypes.String),
              new Claim(ClaimTypes.Email, impersonatedUser.Email, ClaimValueTypes.String)
          };
  
          var user = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
  
          var authenticationManager = _httpContextAccessor.HttpContext.Authentication;
          await authenticationManager.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
          await authenticationManager.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, new AuthenticationProperties() { IsPersistent = false });
  
          return RedirectToAction("Index", "Home");
      }
  </code></pre>
  
  <p>I populated necessary claims and passed it to <code>ClaimsPrincipal</code> so <code>SignInAsync</code> can now take <code>var user</code>.
  However, this seems like I am not actually logging in as the user found in <code>AspNetUsers</code> table who has the role and privileges previously assigned by the Admin role. To be honest, I was expecting above code to at least sign-in as the Name and Surname I defined in <code>var claims</code>, but in fact I am still logged-in as the admin account after I'm redirected to Index page.</p>
  
  <p>What are the proper steps I need to take in order to sign-in as the user account defined in <code>AspNetUsers</code> table so the Admin would be able to browse the application as the user himself?</p>