//[HandleErrorWithLog4net(HandleType = ErrorHandleType.ResponseWrite, ResponseMsg = "网络问题,请稍后再试!")] //[ValidateAntiForgeryToken(Salt = SystemHelper.AntiForgeryTokenSalt)] public ActionResult Login(Login model) { var srm = new ShowResultModel(); if (ModelState.IsValid) { SystemAdmin user = _systemAdminService.GetByNameAndPassword(model.SAName, model.SAPwd); try { if (user == null) { throw new InvalidOperationException("用户名或密码错误!"); } if (!user.IsEnable) { throw new InvalidOperationException("该账户已被禁用"); } srm.IsSuccess = true; var userModules = _systemAdminService.GetsysAdminModule(user); // user data: var userDate = ";"; if (userModules != null && userModules.Any()) { var ulist = userModules.Where(x => !string.IsNullOrEmpty(x.FormRoleName)) .Select(x => x.FormRoleName) .Distinct() .ToArray(); if (ulist.Any()) { userDate = string.Join(",", ulist) + ";"; } } FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, user.SAName, //user.Name DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), false, //model.RememberMe, // user data: userDate //new string[] { "admin", "corp" }.Aggregate((i, j) => i + "," + j) + ";" + IPHelper.getIPAddr() + ";" + user.SAID + ";" + user.SANickName ); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); cookie.HttpOnly = true; //不能通过客户端脚本访问cookie Response.Cookies.Add(cookie); //登录成功更新访问时间 _systemAdminService.UpdateLogonInfo(user); _adminLogService.Log(user, "管理员登录", "帐号:" + user.SAName + " || 姓名:" + user.SANickName + " || 上次访问IP:" + user.LastIP + " || 上次访问时间:" + user.LastTime + " || 当前访问IP:" + user.CurrentIP + " || 当前访问时间:" + user.CurrentTime + " || 登录次数:" + user.LoginTimes); } catch (InvalidOperationException e) { srm.TipMsg = e.Message; } catch (Exception) { srm.TipMsg = "网络错误,请稍后再试!"; } finally { _adminCacheService.Remove(AdminCacheService.SysAdmin_Current_prefix + user.SAName); _adminCacheService.Add(AdminCacheService.SysAdmin_Current_prefix + user.SAName, user, TimeSpan.FromHours(2)); } } else { srm.TipMsg = "数据有效性验证失败!"; } return(Json(srm)); }