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.
134 lines
5.5 KiB
134 lines
5.5 KiB
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<Person> _personRepo;
|
|
private readonly IRepository<PersonLogin> _userRepo;
|
|
private readonly IRepository<PersonRole> _personRoleRepo;
|
|
private readonly IRepository<SystemRole> _roleRepo;
|
|
|
|
public AccountController(IEncryptionService encryptionService,
|
|
IRepository<Person> personRepo,
|
|
IRepository<PersonLogin> userRepo,
|
|
IRepository<PersonRole> personRoleRepo,
|
|
IRepository<SystemRole> 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<Claim> {
|
|
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<LoginModel>(model);
|
|
}
|
|
|
|
public IActionResult Logout()
|
|
{
|
|
Request.HttpContext.JwtSignOut();
|
|
return RedirectToAction("Index", "Home");
|
|
}
|
|
|
|
protected IActionResult Result<TEditModel>(object model)
|
|
{
|
|
if (this.IsJsonRequest())
|
|
{
|
|
return Json(new
|
|
{
|
|
schema = this.GetJsonSchema<TEditModel>(),
|
|
model,
|
|
errors = ModelState.Where(o => o.Value.ValidationState == ModelValidationState.Invalid),
|
|
data = ViewData
|
|
}, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
|
}
|
|
return View(model);
|
|
}
|
|
}
|
|
} |