Skip to content

Instantly share code, notes, and snippets.

@fmshk97
Created February 19, 2021 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmshk97/7e7ec7fa8124cf5e5b48f4ea4a254253 to your computer and use it in GitHub Desktop.
Save fmshk97/7e7ec7fa8124cf5e5b48f4ea4a254253 to your computer and use it in GitHub Desktop.
Login API implementation in AccountsController
[HttpPost]
public async Task<IActionResult> Login([FromBody] LoginInputModel loginInput)
{
var user = await _userManager.FindByNameAsync(loginInput.UserName);
if (user != null && await _userManager.CheckPasswordAsync(user, loginInput.Password))
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
return Ok("Login successful");
}
return BadRequest("Invalid Username or Password");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment