/// <summary> /// 密码默认初始化为666666 /// </summary> /// <param name="name">用户名</param> /// <returns></returns> public ActionResult resetUserPwd(string name = "", int id = 0, string newpwd = "") { var state = System.Configuration.ConfigurationManager.AppSettings["InitPwd"]; if ("1".Equals(state)) { using (WeiQingEntities db = new WeiQingEntities()) { if (id > 0) { var u = db.user.Where(x => x.id == id).FirstOrDefault(); if (u != null && u.id > 0) { if (newpwd != null && newpwd.Length >= 6) { u.pwd = HashTools.SHA1_Hash(newpwd); } else { u.pwd = HashTools.SHA1_Hash("666666"); } int res = db.SaveChanges(); if (res > 0 && string.IsNullOrEmpty(newpwd)) { return(Content("<script>alert('初始化密码成功,密码为666666,请立即修改密码')</script>")); } else if (res > 0) { return(Content("<script>alert('初始化密码成功,密码为" + newpwd + " ,请立即修改密码')</script>")); } } } if (name != null && name.Length > 0) { var u = db.user.Where(x => x.nick_name.Equals(name)).FirstOrDefault(); if (u != null && u.id > 0) { if (newpwd != null && newpwd.Length >= 6) { u.pwd = HashTools.SHA1_Hash(newpwd); } else { u.pwd = HashTools.SHA1_Hash("666666"); } int res = db.SaveChanges(); if (res > 0 && string.IsNullOrEmpty(newpwd)) { return(Content("<script>alert('初始化密码成功,密码为666666,请立即修改密码')</script>")); } else if (res > 0) { return(Content("<script>alert('初始化密码成功,密码为" + newpwd + " ,请立即修改密码')</script>")); } } } } } return(Content("设置失败,请检查是否在应用程序设置中开启了此接口,或者已设置为初始密码")); }
public ActionResult adminLogin(user u) { if (u != null) { // 查询用户的数据,判断权限 reflectModel.setValues(u); if (u.nick_name.Length > 0 && u.pwd.Length > 0) { u.pwd = HashTools.SHA1_Hash(u.pwd); using (WeiQingEntities db = new WeiQingEntities()) { var user = db.user.Where(x => (x.nick_name.Equals(u.nick_name) || x.email.Equals(u.nick_name)) && x.pwd.Equals(u.pwd) && x.state == 1).FirstOrDefault(); if (user != null && user.is_admin && user.id > 0) { Session["user"] = user; string ip = Tools.GetRealIP(); login_log log = new login_log() { uid = (int)user.id, login_ip = ip, login_time = DateTime.Now }; db.login_log.Add(log); db.SaveChanges(); return(Content("1")); } } } return(Content("-2")); } return(Content("-1")); }
public ActionResult updatePwd(UserExt u) { if (u != null) { reflectModel.setValues(u); if (u.nick_name.Length >= 3 && u.pwd.Length >= 6 && u.newpwd.Length >= 6 && !u.pwd.Equals(u.newpwd)) { try { string old_hash_pwd = HashTools.SHA1_Hash(u.pwd); // 旧的hash密码 using (WeiQingEntities db = new WeiQingEntities()) { var user = db.user.Where(p => p.nick_name.Equals(u.nick_name) && p.pwd.Equals(old_hash_pwd)).FirstOrDefault(); if (user != null && u.nick_name.Equals(user.nick_name)) { user.pwd = HashTools.SHA1_Hash(u.newpwd); int res = db.SaveChanges(); // 修改密码 if (res > 0) { Session["user"] = null; } return(Content(res.ToString())); } return(Content("旧密码不正确")); } } catch (Exception ex) { return(Content("后台出现错误")); } } return(Content("修改失败")); } return(Content("参数错误")); }
public ActionResult getPwd(user u) { if (u != null) { reflectModel.setValues(u); if (u.nick_name.Length >= 3 && Tools.IsEmail(u.email)) { try { using (WeiQingEntities db = new WeiQingEntities()) { var user = db.user.Where(p => p.nick_name.Equals(u.nick_name) && p.email.Equals(u.email)).FirstOrDefault(); // 检查用户名和邮箱是否匹配 if (user != null && u.nick_name.Equals(user.nick_name)) { DateTime dt = DateTime.Now; string ip = Tools.GetRealIP(); // 获取客户端ip // 检查当前 uid 一周之内是否已经找回过密码, 同一个ip一天之内之内找回3次密码 var t1 = dt.AddDays(-7); var gpl_uid = db.getpwdlog.Where(p => p.uid == user.id && p.log_time > t1).Count(); if (gpl_uid > 0) { return(Content("一个星期之内只能找回一次密码")); } var t2 = dt.AddHours(-24); var gpl_ip = db.getpwdlog.Where(p => p.ip_address.Equals(ip) && p.log_time > t2).Count(); if (gpl_ip >= 3) { return(Content("同一个ip地址一天之内只能找回3次密码")); } string newpwd = Tools.getRandomStr(); string res = Tools.SendEmail(u.email, "您的密码是:" + newpwd); // 失败返回错误信息 if ("发送成功".Equals(res)) { var chPwdLog = new getpwdlog() { uid = (Int32)user.id, ip_address = ip, nick_name = user.nick_name, log_time = dt }; db.getpwdlog.Add(chPwdLog); // 修改密码的日志 user.pwd = HashTools.SHA1_Hash(newpwd); db.SaveChanges(); // 修改密码 } return(Content(res)); } return(Content("用户名或者邮箱不正确")); } } catch (Exception ex) { return(Content("后台出现错误:" + ex.Message)); } } } return(Content("参数错误")); }
/// <summary> /// 管理员在后台修改用户的资料 /// </summary> /// <param name="u"></param> /// <returns></returns> public ActionResult updateUser(UserExt u) { if (u != null && u.id > 0) { if (u.id == 1 && (u.state == 0 || u.is_admin == false)) { return(Content("超级管理员的权限不能更改")); } var cur_user = (user)Session["user"]; if (u.pwd != null && u.pwd.Length >= 6) { if (u.id == 1 && cur_user.id != 1) { return(Content("超级管理员的密码不能更改")); } u.pwd = HashTools.SHA1_Hash(u.pwd); } else { u.pwd = u.oldpwd; } if (u.email != null && u.email.Length > 0) { if (u.reg_date == DateTime.MinValue) { return(Content("注册时间参数错误")); } reflectModel.setValues(u); try { var model = reflectModel.AutoCopyToBase <user, UserExt>(u); int res = EfExt.Update(model); if (res > 0) { return(Content("1")); } } catch (Exception ex) { return(Content(ex.Message)); } return(Content("修改失败")); } else { return(Content("邮箱不能为空")); } } return(Content("参数错误")); }
public ActionResult login(user u) { if (u != null) { reflectModel.setValues(u); if (u.nick_name.Length >= 3 && u.pwd.Length >= 6) { u.pwd = HashTools.SHA1_Hash(u.pwd); try { using (WeiQingEntities db = new WeiQingEntities()) { var user = db.user.Where(p => (p.nick_name.Equals(u.nick_name) || p.email.Equals(u.nick_name)) && p.pwd.Equals(u.pwd)).FirstOrDefault(); if (user != null && (user.nick_name.Equals(u.nick_name) || user.email.Equals(u.nick_name))) { // 检查用户是否禁止登录,并且判断是否为管理员 if (user.state == 0) { return(Content("你的账号被禁止登录")); } string ip = Tools.GetRealIP(); login_log log = new login_log() { uid = (int)user.id, login_ip = ip, login_time = DateTime.Now }; db.login_log.Add(log); db.SaveChanges(); Session["user"] = user; return(Content("1")); } } } catch (Exception ex) { return(Content("后台出现错误:" + ex.Message)); } } return(Content("用户名和密码不正确")); } return(Content("参数错误")); }
public ActionResult regUser(user u) { if (u != null) { reflectModel.setValues(u); if (Tools.getStrLength(u.nick_name) < 3) { return(Content("用户名的长度必须大于3个字符")); } if (u.pwd.Length < 6) { return(Content("密码必须大于6个字符")); } if (!Tools.IsEmail(u.email)) { return(Content("邮箱格式不正确")); } u.pwd = HashTools.SHA1_Hash(u.pwd); DateTime dt = DateTime.Now; u.reg_date = dt; u.state = 1; int res = 0; try { TransactionOptions transactionOption = new TransactionOptions(); //设置事务隔离级别 transactionOption.IsolationLevel = IsolationLevel.ReadCommitted; // 设置事务超时时间为60秒 transactionOption.Timeout = new TimeSpan(0, 0, 60); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption)) { using (WeiQingEntities db = new WeiQingEntities()) { var count = db.user.Where(p => p.nick_name.Equals(u.nick_name) || p.email.Equals(u.email)).Count(); if (count > 0) { return(Content("此用户名或者邮箱已被注册")); } u.is_admin = false; db.user.Add(u); res = db.SaveChanges(); // 创建用户 if (res == 0) { return(Content("注册失败")); } var user = db.user.Where(p => p.nick_name.Equals(u.nick_name)).FirstOrDefault(); string ip = Tools.GetRealIP(); login_log log = new login_log() { uid = (int)user.id, login_ip = ip, login_time = dt }; db.login_log.Add(log); res = db.SaveChanges(); if (res > 0) { Session["user"] = user; scope.Complete(); } else { return(Content("保存登录记录时出现异常")); } return(Content(res.ToString())); } } } catch (Exception ex) { return(Content("后台出现错误")); } } return(Content("没有获取到数据")); }