LoginController
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using PT.BLL; using PT.Common; using PT.Model; using PT.Web.Mvc.App_Start; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace PT.Web.Mvc.Controllers { public class LoginController : BaseController<UserList, UserListV> { UserListBll bll = new UserListBll(); LoginFailedBll loginFailedBll = new LoginFailedBll(); private SessionHelper sessionHelper; public IActionResult Index() { //DomainAutoLogin2(); return View(); } [LoginAuthorizationFilter] public IActionResult Welcome() { return View(); } public IActionResult LoginAct(UserList t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { LoginFailed loginFailed = new LoginFailed() { LoginID = t.LoginID }; loginFailed = loginFailedBll.QueryableSingle(loginFailed); TimeSpan? ts; if (loginFailed != null) { ts = DateTime.Now - loginFailed.LoginDate; //错误达到5次并锁定时间在10分钟以内 将不能再往下执行 if (loginFailed.FailedNumber >= 5 && ts.Value.TotalMinutes <= 10) { result.code = -3; result.msg = "登录失败已" + loginFailed.FailedNumber + "次,将限制10分钟不能再登录,已过:" + ((int)ts.Value.TotalMinutes) + "分钟!"; return new JsonResult(result); } //错误达到5次并锁定时间超过10分钟 将重置错误次数为0 if (loginFailed.FailedNumber >= 5 && ts.Value.TotalMinutes > 10) { loginFailed.LoginDate = DateTime.Now; loginFailed.FailedNumber = 0; loginFailedBll.SaveLoginFailed(loginFailed); } } else { loginFailed = new LoginFailed(); loginFailed.LoginID = t.LoginID; } sessionHelper = new SessionHelper(HttpContext.Session); t.LoginPwd = EncryptionHelper.GetUserPwd(t.LoginPwd); UserList data = bll.QueryableSingle(t); if (data != null) { if (data.Active) { loginFailed.LoginDate = DateTime.Now; loginFailed.FailedNumber = 0; //登录成功 将重置错误次数为0 loginFailedBll.SaveLoginFailed(loginFailed); ts = DateTime.Now - data.UpdatePwdDate; //首次登录没有改过密码或密码90天未修改则跳转到修改密码 if (!data.UpdatePwdDate.HasValue || ts.Value.Days > 90) { result.code = -4; result.msg = "首次登录没有改过密码或密码超过90天未修改,请修改密码!"; sessionHelper.SetEntity<UserList>("UserUpdatePwd", data); } else { result.data = data; sessionHelper.SetUserInfoString(data); AddSystemLog(new SystemLog() { OperateType = "logon", OperateModule = "Logon", OperateFunction = "账号密码登录", OperatePage = "登录:Login/Index" }); } } else { result.code = -2; result.msg = "当前Login ID未激活!"; } } else { result.code = -1; result.msg = "账号密码错误!"; loginFailed.LoginDate = DateTime.Now; loginFailed.FailedNumber = loginFailed.FailedNumber + 1; //登录失败 将错误次数+1 loginFailedBll.SaveLoginFailed(loginFailed); loginFailed = loginFailedBll.QueryableSingle(loginFailed); if (loginFailed.FailedNumber >= 3) { result.code = -3; result.msg = "登录失败已" + loginFailed.FailedNumber + "次,失败5次将限制10分钟不能再登录!"; } } } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 检查是否满足 域名账号自动登录 /// </summary> /// <returns></returns> public IActionResult DomainAutoLoginCheck() { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { //获取本机域账号的几种方式 System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); string strUserName = currentUser.Name.ToString(); ViewData["currentUser"] = strUserName; string a = HttpContext.GetServerVariable("LOGON_USER"); ViewData["a"] = a; string b = HttpContext.GetServerVariable("Remote_Host"); ViewData["b"] = b; string c = User.Identity.Name; ViewData["c"] = c; if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = a; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = b; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = c; } UserList t = new UserList(); if (!string.IsNullOrWhiteSpace(strUserName)) { t.DomainAccount = strUserName.Substring(strUserName.IndexOf('\\') + 1); } ViewData["DomainAccount"] = t.DomainAccount; UserList data = bll.LoginAct(t); if (data != null) { result.code = 1; result.msg = "满足自动登录"; result.data = "strUserName:" + strUserName + ",a:" + a + ",b:" + b + ",c:" + c + ",DomainAccount:" + t.DomainAccount; } } catch (Exception ex) { result.code = -1; result.msg = ex.ToString(); } var res = new JsonResult(result); return res; } /// <summary> /// 域名账号自动登录 /// </summary> /// <returns></returns> public IActionResult DomainAutoLogin() { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { sessionHelper = new SessionHelper(HttpContext.Session); //获取本机域账号的几种方式 System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); string strUserName = currentUser.Name.ToString(); ViewData["currentUser"] = strUserName; string a = HttpContext.GetServerVariable("LOGON_USER"); ViewData["a"] = a; string b = HttpContext.GetServerVariable("Remote_Host"); ViewData["b"] = b; string c = User.Identity.Name; ViewData["c"] = c; if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = a; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = b; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = c; } UserList t = new UserList(); if (!string.IsNullOrWhiteSpace(strUserName)) { t.DomainAccount = strUserName.Substring(strUserName.IndexOf('\\') + 1); } ViewData["DomainAccount"] = t.DomainAccount; UserList data = bll.LoginAct(t); if (data != null) { if (data.Active) { result.data = data; sessionHelper.SetUserInfoString(data); AddSystemLog(new SystemLog() { OperateType = "logon", OperateModule = "Logon", OperateFunction = "APO自动登录", OperatePage = "登录:Login/Index" }); } else { result.code = -3; result.msg = "账号未激活"; } } else { result.code = -2; result.msg = "未能自动登录"; } } catch (Exception ex) { result.code = -1; result.msg = ex.ToString(); } var res = new JsonResult(result); return res; } /// <summary> /// 域名账号自动登录 /// </summary> /// <returns></returns> public void DomainAutoLogin2() { try { sessionHelper = new SessionHelper(HttpContext.Session); //获取本机域账号的几种方式 System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); string strUserName = currentUser.Name.ToString(); ViewData["currentUser"] = strUserName; string a = HttpContext.GetServerVariable("LOGON_USER"); ViewData["a"] = a; string b = HttpContext.GetServerVariable("Remote_Host"); ViewData["b"] = b; string c = User.Identity.Name; ViewData["c"] = c; if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = a; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = b; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = c; } UserList t = new UserList(); t.DomainAccount = strUserName.Substring(strUserName.IndexOf('\\') + 1); ViewData["DomainAccount"] = t.DomainAccount; UserList data = bll.LoginAct(t); if (data != null) { if (data.Active) { sessionHelper.SetUserInfoString(data); Response.Redirect("/IPList/Index"); } } } catch (Exception ex) { } } public IActionResult Logout(UserList t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { sessionHelper = new SessionHelper(HttpContext.Session); HttpContext.Session.Clear(); return View("Index"); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } public IActionResult UpdatePwd() { return View(); } /// <summary> /// 保存修改密码 /// </summary> /// <param name="t"></param> /// <returns></returns> public IActionResult SaveUpdatePwd(UserListV2 t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; var res = new JsonResult(result); try { sessionHelper = new SessionHelper(HttpContext.Session); UserList user = sessionHelper.GetEntity<UserList>("UserUpdatePwd"); t.LoginID = user.LoginID; t.LoginPwd = EncryptionHelper.GetUserPwd(t.LoginPwd); if (t.LoginPwd != user.LoginPwd) { result.code = -2; result.msg = "原密码错误!"; res = new JsonResult(result); return res; } t.Id = user.Id; //t.UpdatePwdNumber = user.UpdatePwdNumber + 1; t.UpdatePwdDate = DateTime.Now; t.LoginPwd = EncryptionHelper.GetUserPwd(t.NewLoginPwd); result.data = bll.UpdatePwd(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } res = new JsonResult(result); return res; } } }