You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/labs/Teacher/TeacherExt/Controllers/AccountController.cs

61 lines
1.8 KiB

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<User> _userRepo;
public AccountController(IEncryptionService encryptionService,IRepository<User> 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");
}
}
}