AccountController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.AspNetCore.Authentication;
  2. using Microsoft.AspNetCore.Authentication.Cookies;
  3. using Microsoft.AspNetCore.Identity;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.CodeAnalysis.Operations;
  6. using ProductionLineMonitor.Application.Services.AdminService;
  7. using ProductionLineMonitor.Core.Dtos;
  8. using ProductionLineMonitor.Core.IRepositories;
  9. using ProductionLineMonitor.Core.Models;
  10. using ProductionLineMonitor.Core.Utils;
  11. using System.Collections.Generic;
  12. using System.Security.Claims;
  13. namespace ProductionLineMonitor.Web.Controllers
  14. {
  15. public class AccountController : Controller
  16. {
  17. private readonly IAdminService _adminService;
  18. public AccountController(
  19. IAdminService adminService)
  20. {
  21. _adminService = adminService;
  22. }
  23. [HttpGet]
  24. public IActionResult Login()
  25. {
  26. return View();
  27. }
  28. [HttpPost]
  29. public IActionResult Login(string username, string password)
  30. {
  31. if (string.IsNullOrEmpty(username))
  32. {
  33. ModelState.AddModelError("error", "请输入工号!");
  34. return View(ModelState);
  35. }
  36. if (string.IsNullOrEmpty(password))
  37. {
  38. ModelState.AddModelError("error", "请输入密码!");
  39. return View(ModelState);
  40. }
  41. var userLoginInfo = _adminService.Login(username, password);
  42. if (userLoginInfo == null)
  43. {
  44. ModelState.AddModelError("error", "工号或密码不正确!");
  45. return View(ModelState);
  46. }
  47. if (password == "123456")
  48. {
  49. userLoginInfo.IsInitialPassword = true;
  50. return Redirect("/Account/ChangePassword/" + userLoginInfo.JobNo);
  51. }
  52. var claims = new List<Claim>
  53. {
  54. new Claim(ClaimTypes.Name, userLoginInfo.JobNo),
  55. new Claim(ClaimTypes.UserData, userLoginInfo.ToJson())
  56. };
  57. var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  58. HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
  59. if (userLoginInfo.MenuDtos.Count > 0)
  60. return Redirect(userLoginInfo.MenuDtos[0].Url);
  61. else
  62. return RedirectToAction("Index", "Home");
  63. }
  64. [HttpGet]
  65. public IActionResult LoginOut()
  66. {
  67. HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
  68. return RedirectToAction("Login", "Account");
  69. }
  70. [HttpGet("/Account/ChangePassword/{jobNo}")]
  71. public IActionResult ChangePassword([FromRoute] string jobNo)
  72. {
  73. ViewBag.JobNo = jobNo;
  74. return View();
  75. }
  76. [HttpPost("/Account/ChangePassword")]
  77. public IActionResult ChangePassword(string jobNo, string password, string newPassword)
  78. {
  79. if (string.IsNullOrEmpty(jobNo))
  80. {
  81. ModelState.AddModelError("error", "请输入工号!");
  82. ViewBag.JobNo = jobNo;
  83. return View(ModelState);
  84. }
  85. if (string.IsNullOrEmpty(password))
  86. {
  87. ModelState.AddModelError("error", "请输入密码!");
  88. ViewBag.JobNo = jobNo;
  89. return View(ModelState);
  90. }
  91. if (string.IsNullOrEmpty(newPassword))
  92. {
  93. ModelState.AddModelError("error", "请输入新密码!");
  94. ViewBag.JobNo = jobNo;
  95. return View(ModelState);
  96. }
  97. var result = _adminService.ChangePassword(new Core.Dtos.UserChangePasswordDto()
  98. {
  99. JobNo = jobNo,
  100. Password = password,
  101. NewPassword = newPassword,
  102. AgainNewPassword = newPassword
  103. });
  104. if (result.Code == Core.Dtos.CodeEnum.Fail)
  105. {
  106. ModelState.AddModelError("error", result.Message);
  107. ViewBag.JobNo = jobNo;
  108. return View(ModelState);
  109. }
  110. return Redirect("/Account/Login");
  111. }
  112. [HttpGet]
  113. public IActionResult InternalLogin(string jobNo, string url)
  114. {
  115. var userLoginInfo = _adminService.Login(jobNo);
  116. if (userLoginInfo == null)
  117. return RedirectToAction("Login", "Account");
  118. var claims = new List<Claim>
  119. {
  120. new Claim(ClaimTypes.Name, userLoginInfo.JobNo),
  121. new Claim(ClaimTypes.UserData, userLoginInfo.ToJson())
  122. };
  123. var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  124. HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
  125. return Redirect(url);
  126. }
  127. }
  128. }