using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Security; using Microsoft.AspNetCore.Mvc; using System.Linq; using TeacherExt.Entities; using TeacherExt.Models; namespace TeacherExt.Controllers { public class AccountController : Controller { private readonly IEncryptionService _encryptionService; private readonly IRepository _userRepo; public AccountController(IEncryptionService encryptionService,IRepository userRepo) { this._encryptionService = encryptionService; this._userRepo = userRepo; } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] public IActionResult Login(LoginModel model) { if (ModelState.IsValid) { var user = this._userRepo.ReadOnlyTable().FirstOrDefault(o => o.UserName == model.UserName); if (user!=null) { if (user.PasswordHash == this._encryptionService.CreatePasswordHash(user.PasswordSalt, model.Password)) { Request.HttpContext.JwtSignIn(model.UserName, model.RememberMe); return RedirectToAction("Index","Home"); } else { ModelState.AddModelError(nameof(model.Password), "密码错误"); } } else { ModelState.AddModelError(nameof(model.UserName), "用户不存在"); } } return View(); } public IActionResult Logout() { Request.HttpContext.JwtSignOut(); return RedirectToAction("Index", "Home"); } } }