In order to make user authentication works, The app should have endpoints for Login & Logout.
we can utilize SignInManager service from AspNetCore.Identity.
The AccountController willl look like:
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IUserProfileService _userProfileService;
public AccountController(UserManager<ApplicationUser> userManager,
IUserProfileService userProfileService,
SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_userProfileService = userProfileService;
_signInManager = signInManager;
}
[HttpPost("Login")]
public async Task<IActionResult> Login([FromBody] LoginDto model)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);
if (result.Succeeded)
{
return Ok("You have been successfully logged in");
}
return BadRequest("Login failed");
}
[HttpPost("Logout")]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return Ok("You have been successfully logged out");
}
.........
}
@affand Thanks for suggesting this, but it isn't intended to use Login/Logout mechanism for authentication. It uses token based auth, which it must include a previously retrieve token in every request.
In order to make user authentication works, The app should have endpoints for Login & Logout. we can utilize SignInManager service from AspNetCore.Identity.
The AccountController willl look like: