File size: 2,057 Bytes
fc06b79 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | using ContactManagementAPI.Data;
using ContactManagementAPI.Models;
using ContactManagementAPI.Services;
using ContactManagementAPI.ViewModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ContactManagementAPI.Controllers
{
public class AccountController : Controller
{
private readonly ApplicationDbContext _context;
private readonly PasswordHasher<AppUser> _passwordHasher = new();
public AccountController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public IActionResult Login(string? returnUrl = null)
{
return View(new LoginViewModel { ReturnUrl = returnUrl });
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginViewModel model)
{
if (!ModelState.IsValid)
return View(model);
var user = _context.AppUsers
.Include(u => u.Group)
.FirstOrDefault(u => u.UserName == model.UserName);
if (user == null || !user.IsActive)
{
ModelState.AddModelError(string.Empty, "Invalid username or password.");
return View(model);
}
var result = _passwordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password);
if (result == PasswordVerificationResult.Failed)
{
ModelState.AddModelError(string.Empty, "Invalid username or password.");
return View(model);
}
HttpContext.Session.SetInt32(SessionKeys.UserId, user.Id);
return Redirect(string.IsNullOrWhiteSpace(model.ReturnUrl) ? "/" : model.ReturnUrl);
}
public IActionResult Logout()
{
HttpContext.Session.Clear();
return RedirectToAction("Login");
}
public IActionResult AccessDenied()
{
return View();
}
}
}
|