Created
February 19, 2021 15:35
-
-
Save fmshk97/7e7ec7fa8124cf5e5b48f4ea4a254253 to your computer and use it in GitHub Desktop.
Login API implementation in AccountsController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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