using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Security; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using TeacherExt.Entities; using TeacherExt.Models; namespace TeacherExt.Controllers { [EnableCors] public class AccountController : Controller { private readonly IEncryptionService _encryptionService; private readonly IRepository _personRepo; private readonly IRepository _userRepo; private readonly IRepository _personRoleRepo; private readonly IRepository _roleRepo; public AccountController(IEncryptionService encryptionService, IRepository personRepo, IRepository userRepo, IRepository personRoleRepo, IRepository roleRepo) { this._encryptionService = encryptionService; this._personRepo = personRepo; this._userRepo = userRepo; this._personRoleRepo = personRoleRepo; this._roleRepo = roleRepo; } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] public IActionResult Login(LoginModel model) { if (ModelState.IsValid) { var user = this._userRepo.ReadOnlyTable().FirstOrDefault(o => o.LoginName == model.UserName); if (user != null) { if (!string.IsNullOrEmpty(model.Password) && user.Password != model.Password.Md5()) { ModelState.AddModelError(nameof(model.Password), "密码错误"); } else { var organId = this._personRepo.ReadOnlyTable().Where(o => o.Id == user.PersonId).Select(o=>o.OrganId).FirstOrDefault(); var roles = (from person in this._personRoleRepo.ReadOnlyTable().Where(o => o.PersonId == user.PersonId) join role in this._roleRepo.ReadOnlyTable() on person.RoleId equals role.Id select role.RoleName).Distinct().ToList(); var claims = new List { new Claim(ClaimTypes.Name, model.UserName) , new Claim(ClaimTypes.GivenName, user.RealName) , new Claim("PersonId", Convert.ToString(user.PersonId)), new Claim("OrganId", Convert.ToString(organId)) , new Claim(ClaimTypes.NameIdentifier,user.PersonId.ToString()) }; foreach (var item in roles) { claims.Add(new Claim(ClaimTypes.Role, item)); } var httpContext = this.Request.HttpContext; var token = httpContext.CreateJwtToken(claims, DateTime.Now.AddYears(1)); // var cookieOptions = new CookieOptions { HttpOnly = true }; if (model.RememberMe) { cookieOptions.Expires = DateTimeOffset.Now.AddYears(1); } var cookieName = httpContext.GetJwtCookieName(); httpContext.Response.Cookies.Delete(cookieName); httpContext.Response.Cookies.Append(cookieName, token, cookieOptions); if (!Request.IsAjax()) { return RedirectToAction("Index", "Home"); } else { return Json(new { AccessToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddYears(100)), RefreshToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddYears(100)), }); } } } else { ModelState.AddModelError(nameof(model.UserName), "用户不存在"); } } return Result(model); } public IActionResult Logout() { Request.HttpContext.JwtSignOut(); return RedirectToAction("Index", "Home"); } protected IActionResult Result(object model) { if (this.IsJsonRequest()) { return Json(new { schema = this.GetJsonSchema(), model, errors = ModelState.Where(o => o.Value.ValidationState == ModelValidationState.Invalid), data = ViewData }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } return View(model); } } }