1

I am using ASP.NET Core 2.2 and ASP.NET Identity on an API.

Is it possible to Manually Authenticate a User with Claims for every request?

I would like to use this during initial development stages ...

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

1

For authenticating a user without logging, you could try like:

public async Task<IActionResult> Login()
{
    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "edward"));
    identity.AddClaim(new Claim(ClaimTypes.Name, "edward zhou"));
    //add your own claims 
    var principal = new ClaimsPrincipal(identity);
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });            
    return View();
}
Edward
  • 28,296
  • 11
  • 76
  • 121