public IHttpActionResult Login(LoginModel model) { if (model == null) { return(Failure("用户名不存在")); } if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password)) { return(Failure("用户名或密码不得为空")); } //var serial = ConfigurationManager.AppSettings["SerialNumber"]; //if (string.IsNullOrEmpty(serial)) //{ // return Failure("请联系销售获取产品序列号"); //} //DateTime dt = DateTime.Now; //if (!DateTime.TryParse(AESAlgorithm.Decrypto(serial),out dt)||dt < DateTime.Now) //{ // return Failure("序列号已经过期,请联系销售获取最新序列号"); //} try { lock (ContinueErrorPassword) { //校验5次密码错误 ContinueErrorPassword.RemoveAll(m => m.CreateTime.Day != DateTime.Now.Day); if (ContinueErrorPassword.Count(m => m.UserName == model.UserName) > 5) { UserInfo u = UserInfo.GetOne(model.UserName); if (u != null) { u.Status = 0; UserInfo.Update(u); } return(Failure("连续输错5次密码并冻结")); } } UserInfo user = UserInfo.GetOne(model.UserName); if (user == null) { return(Failure("用户名不存在")); } if (user.Status == 0) { return(Failure("此用户已禁用,请联系管理员")); } if (user.Password != AESAlgorithm.Encrypto(model.Password)) { model.CreateTime = DateTime.Now; lock (ContinueErrorPassword) { ContinueErrorPassword.Add(model); } return(Failure("密码错误")); } var token = user.ID.ToString(); SystemLog.Add(new SystemLog { Action = "Login", LogContent = user.Name + "-登录成功", CreateTime = DateTime.Now, UserID = user.ID, RoleID = user.RoleID, DepartmentID = user.DepartmentID, ClientIP = GetIP(), UserName = user.Name, RealName = user.RealName }); Department depart = Department.GetOne(user.DepartmentID); if (user.LastPasswordTime == null) { user.LastPasswordTime = DateTime.Now; } var data = new { UserID = user.ID, RoleName = user.RoleID == 1 ? "admin" : "user",//1是超管,2是用户 RealName = user.RealName, DepartmentName = depart?.Name, NeedChangePassword = (user.LastPasswordTime.Value.AddDays(7) < DateTime.Now ? true : false), //是否需要提示修改密码 RoleModel = Role_Module.Get(user.RoleID), //返回所有模块 }; WriteCookie("token", token); user.LastLoginTime = DateTime.Now; if (!LoginDictionary.ContainsKey(token)) { LoginDictionary.Add(token, user); } _logger.Info(string.Join(",", LoginDictionary.Keys.ToList())); return(Success(data));//返回用户权限 }catch (Exception e) { _logger.Error(e); return(Failure(e.Message)); } }
private void InitDB() { #region 初始化部门 Department depart = Department.GetOne("根组织"); if (depart == null) { Department.Add(new Department { Name = "根组织", ParentID = null, Remark = "不可删除", SortID = 0 }); depart = Department.GetOne("根组织"); } #endregion #region 初始化管理员 UserInfo user = UserInfo.GetOne("admin"); if (user == null) { UserInfo.Add(new UserInfo { Name = "admin", RoleID = 1, DepartmentID = depart.ID, Password = CabinetUtility.Encryption.AESAlgorithm.Encrypto("admin"), RealName = "管理员", CreateTime = DateTime.Now, Status = 1, LastPasswordTime = DateTime.Now }); } #endregion #region 初始化权限 if (Role_Module.Get(1).Count == 0) { List <Role_Module> roleList = new List <Role_Module>(); for (int i = 1; i < 5; i++) { for (int j = 0; j < 12; j++) { Role_Module role = new CabinetData.Entities.Role_Module(); role.RoleID = i; role.ModuleID = j; role.ModuleName = Enum.GetName(typeof(ModuleEnum), j); role.EnableAdd = true; role.EnableDelete = true; role.EnableEdit = true; role.EnableView = true; roleList.Add(role); } } Role_Module.Insert(roleList); } #endregion #region 新增字段 UserInfo.AddColumn(); #endregion }