123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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<Claim>
- {
- 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<Claim>
- {
- 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);
- }
- }
- }
|