using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.CodeAnalysis.Operations; using ProductionLineMonitor.Application.Services.AdminService; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Utils; using System.Collections.Generic; using System.Security.Claims; namespace ProductionLineMonitor.Web.Controllers { public class AccountController : Controller { private readonly IAdminService _adminService; public AccountController( IAdminService adminService) { _adminService = adminService; } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] public IActionResult Login(string username, string password) { if (string.IsNullOrEmpty(username)) { ModelState.AddModelError("error", "请输入工号!"); return View(ModelState); } if (string.IsNullOrEmpty(password)) { ModelState.AddModelError("error", "请输入密码!"); return View(ModelState); } var userLoginInfo = _adminService.Login(username, password); if (userLoginInfo == null) { ModelState.AddModelError("error", "工号或密码不正确!"); return View(ModelState); } if (password == "123456") { userLoginInfo.IsInitialPassword = true; return Redirect("/Account/ChangePassword/" + userLoginInfo.JobNo); } var claims = new List { new Claim(ClaimTypes.Name, userLoginInfo.JobNo), new Claim(ClaimTypes.UserData, userLoginInfo.ToJson()) }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); if (userLoginInfo.MenuDtos.Count > 0) return Redirect(userLoginInfo.MenuDtos[0].Url); else return RedirectToAction("Index", "Home"); } [HttpGet] public IActionResult LoginOut() { HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Login", "Account"); } [HttpGet("/Account/ChangePassword/{jobNo}")] public IActionResult ChangePassword([FromRoute] string jobNo) { ViewBag.JobNo = jobNo; return View(); } [HttpPost("/Account/ChangePassword")] public IActionResult ChangePassword(string jobNo, string password, string newPassword) { if (string.IsNullOrEmpty(jobNo)) { ModelState.AddModelError("error", "请输入工号!"); ViewBag.JobNo = jobNo; return View(ModelState); } if (string.IsNullOrEmpty(password)) { ModelState.AddModelError("error", "请输入密码!"); ViewBag.JobNo = jobNo; return View(ModelState); } if (string.IsNullOrEmpty(newPassword)) { ModelState.AddModelError("error", "请输入新密码!"); ViewBag.JobNo = jobNo; return View(ModelState); } var result = _adminService.ChangePassword(new Core.Dtos.UserChangePasswordDto() { JobNo = jobNo, Password = password, NewPassword = newPassword, AgainNewPassword = newPassword }); if (result.Code == Core.Dtos.CodeEnum.Fail) { ModelState.AddModelError("error", result.Message); ViewBag.JobNo = jobNo; return View(ModelState); } return Redirect("/Account/Login"); } [HttpGet] public IActionResult InternalLogin(string jobNo, string url) { var userLoginInfo = _adminService.Login(jobNo); if (userLoginInfo == null) return RedirectToAction("Login", "Account"); var claims = new List { new Claim(ClaimTypes.Name, userLoginInfo.JobNo), new Claim(ClaimTypes.UserData, userLoginInfo.ToJson()) }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return Redirect(url); } } }