public ActionResult <string> GetMd5(string source) { return(EncryptMd5.EncryptByte(source)); }
//登录 public Message AdminLogin(AdminLogin adminLogin) { var msg = new Message(10, ""); if (string.IsNullOrEmpty(adminLogin.UserName) || string.IsNullOrEmpty(adminLogin.Password)) { msg.Code = 101; msg.Msg = "用户名或密码不能为空"; return(msg); } if (adminLogin.UserName.Length > 32) { msg.Code = 101; msg.Msg = "用户名或密码输入错误"; return(msg); } if (string.IsNullOrEmpty(adminLogin.VerifyCode)) { msg.Code = 102; msg.Msg = "验证码不能为空"; return(msg); } if (adminLogin.VerifyCode.Length > 6) { msg.Code = 102; msg.Msg = "验证码输入错误"; return(msg); } var validate = HttpExtension.EqualsSessionValue(Consts.Session_ValidateCode, adminLogin.VerifyCode); HttpExtension.RemoveSession(Consts.Session_ValidateCode); if (!validate) { msg.Code = 103; msg.Msg = "验证码错误"; return(msg); } var admin = this.GetAdminByUserName(adminLogin.UserName); if (admin == null || admin.ID <= 0) { msg.Code = 11; msg.Msg = "用户名或密码错误"; return(msg); } if (admin.State == 2) { msg.Code = 12; msg.Msg = "用户已禁用"; return(msg); } var timeStamp = (int)DateTimeUtils.DateTimeToUnixTimeStamp(DateTime.Now); if (admin.LockTime > timeStamp) { msg.Code = 13; msg.Msg = $"帐号已锁定,请{LogonSettings.Value.LockMinute}分钟后再来登录"; return(msg); } //角色是否禁用 var role = this.GetRoleByID(admin.RoleID); if (role == null || role.ID <= 0 || role.State == 2) { msg.Code = 12; msg.Msg = "用户角色禁用,请联系管理员处理"; return(msg); } adminLogin.Password = EncryptMd5.EncryptByte(adminLogin.Password); if (!admin.Password.Equals(adminLogin.Password)) { if (admin.ErrorLogonTime + (LogonSettings.Value.ErrorTime * 60) < timeStamp) { admin.ErrorLogonTime = timeStamp; admin.ErrorLogonCount = 1; } else { admin.ErrorLogonCount += 1; } if (admin.ErrorLogonCount >= LogonSettings.Value.ErrorCount) { admin.ErrorLogonTime = 0; admin.ErrorLogonCount = 0; admin.LockTime = timeStamp + (LogonSettings.Value.LockMinute * 60); //锁定帐号 this.LockAdmin(admin.ID, admin.LockTime); msg.Code = 14; msg.Msg = $"帐号或密码在{LogonSettings.Value.ErrorTime}分钟内,错误{LogonSettings.Value.ErrorCount}次,锁定帐号{LogonSettings.Value.LockMinute}分钟"; return(msg); } else { //更新错误登录信息 this.UpdateErrorLogon(admin.ID, admin.ErrorLogonTime, admin.ErrorLogonCount); msg.Code = 15; msg.Msg = $"帐号或密码错误,如在{LogonSettings.Value.ErrorTime}分钟内,错误{LogonSettings.Value.ErrorCount}次,将锁定帐号{LogonSettings.Value.LockMinute}分钟"; return(msg); } } admin.LastLogonTime = timeStamp; admin.ErrorLogonTime = 0; admin.ErrorLogonCount = 0; admin.LockTime = 0; admin.LastLogonIP = HttpExtension.GetUserIP(); //更新账号登录信息 this.UpdateAdminLogon(admin.ID, admin.LastLogonTime, admin.LastLogonIP); CMSAdminCookie.SetAdiminCookie(adminLogin); msg.Code = 0; msg.Msg = "登录成功"; return(msg); }
//更新账号 public Message UpdateAdminByID(Admin admin) { var msg = new Message(10, ""); if (string.IsNullOrEmpty(admin.UserName.Trim())) { msg.Code = 101; msg.Msg = "用户名不能为空"; return(msg); } if (admin.UserName.Length > 32) { msg.Code = 101; msg.Msg = "用户名长度不能多于32个字符"; return(msg); } var upAdmin = this.GetAdminByID(admin.ID); if (upAdmin == null || upAdmin.ID <= 0) { msg.Code = 11; msg.Msg = "修改的账号不存在"; return(msg); } upAdmin = this.GetAdminByUserName(admin.UserName); if (upAdmin != null && upAdmin.ID != admin.ID) { msg.Code = 12; msg.Msg = "修改的账号用户名已存在"; return(msg); } if (!string.IsNullOrEmpty(admin.Password.Trim())) { if (!admin.Password.Trim().Equals(admin.RePassword)) { msg.Code = 102; msg.Msg = "两次密码不一致"; return(msg); } var pwdMsg = this.VerifyPassword(admin.Password); if (!pwdMsg.Success) { return(msg); } admin.Password = EncryptMd5.EncryptByte(admin.Password.Trim()); } if (admin.RoleID <= 0) { msg.Code = 103; msg.Msg = "请选择账号的角色"; return(msg); } admin.UpdateTime = (int)DateTimeUtils.DateTimeToUnixTimeStamp(DateTime.Now); var upState = CMSAdminDao.UpdateAdminByID(admin.ID, admin.UserName, admin.Password, admin.State, admin.RoleID, admin.UpdateTime); if (upState) { msg.Code = 0; msg.Msg = "修改账号成功"; } else { msg.Code = 1; msg.Msg = "修改账号失败"; } return(msg); }
//修改密码 public Message UpdatePasswordByID(int id, string oldPassword, string password, string rePassword) { var msg = new Message(10, ""); var admin = this.GetAdminByID(id); if (admin == null || admin.ID <= 0) { msg.Code = 11; msg.Msg = "修改的账号不存在"; return(msg); } if (string.IsNullOrEmpty(oldPassword)) { msg.Code = 101; msg.Msg = "旧密码不能为空"; return(msg); } oldPassword = EncryptMd5.EncryptByte(oldPassword); if (!oldPassword.Equals(admin.Password)) { msg.Code = 12; msg.Msg = "旧密码输入错误"; return(msg); } if (string.IsNullOrEmpty(password.Trim()) || !password.Trim().Equals(rePassword)) { msg.Code = 102; msg.Msg = "密码为空或两次密码不一致"; return(msg); } var pwdMsg = this.VerifyPassword(password); if (!pwdMsg.Success) { return(msg); } password = EncryptMd5.EncryptByte(password.Trim()); var updateTime = (int)DateTimeUtils.DateTimeToUnixTimeStamp(DateTime.Now); var upState = CMSAdminDao.UpdatePasswordByID(id, password, updateTime); if (upState) { msg.Code = 0; msg.Msg = "修改密码成功"; } else { msg.Code = 1; msg.Msg = "修改密码失败"; } return(msg); }
//添加账号 public Message CreateAdmin(Admin admin) { var msg = new Message(10, ""); if (admin == null) { msg.Code = 101; msg.Msg = "账号不能为空"; return(msg); } if (string.IsNullOrEmpty(admin.UserName.Trim())) { msg.Code = 102; msg.Msg = "用户名不能为空"; return(msg); } if (admin.UserName.Length > 32) { msg.Code = 102; msg.Msg = "用户名长度不能多于32个字符"; return(msg); } var uAdmin = CMSAdminDao.GetAdminByUserName(admin.UserName); if (uAdmin != null && uAdmin.ID > 0) { msg.Code = 11; msg.Msg = "添加的账号用户名已存在"; return(msg); } if (string.IsNullOrEmpty(admin.Password.Trim()) || !admin.Password.Trim().Equals(admin.RePassword)) { msg.Code = 103; msg.Msg = "密码为空或两次密码不一致"; return(msg); } var pwdMsg = this.VerifyPassword(admin.Password); if (!pwdMsg.Success) { return(msg); } if (admin.RoleID <= 0) { msg.Code = 104; msg.Msg = "请选择账号的角色"; return(msg); } admin.Password = EncryptMd5.EncryptByte(admin.Password.Trim()); admin.CreateTime = (int)DateTimeUtils.DateTimeToUnixTimeStamp(DateTime.Now); admin.UpdateTime = (int)DateTimeUtils.DateTimeToUnixTimeStamp(DateTime.Now); var addState = CMSAdminDao.CreateAdmin(admin); if (addState) { msg.Code = 0; msg.Msg = "添加账号成功"; } else { msg.Code = 1; msg.Msg = "添加账号失败"; } return(msg); }