コード例 #1
0
 public JsonResult AddAdmin(Models.AdminModel model)
 {
     if (ModelState.IsValid)
     {
         IDAL.IAdminRepository adminRepository = EnterRepository.GetRepositoryEnter().GetAdminRepository;
         //判断权限名称是否已存在
         var result = adminRepository.LoadEntities(m => m.Mobile == model.Mobile.Trim()).FirstOrDefault();
         if (result == null)
         {
             Random rn   = new Random();
             string salt = rn.Next(10000, 99999).ToString() + rn.Next(10000, 99999).ToString();
             adminRepository.AddEntity(new Model.Admin()
             {
                 AuthoryId     = model.AuthoryId,
                 CreateTime    = DateTime.Now,
                 IsLogin       = model.IsLogin,
                 Mobile        = model.Mobile,
                 IsSuperAdmin  = 0,
                 LastLoginTime = DateTime.Now,
                 Salt          = salt,
                 Password      = MD5Helper.CreatePasswordMd5("ad" + model.Mobile.Substring(7, 4), salt)
             });
             //添加下操作记录
             PublicFunction.AddOperation(1, string.Format("添加管理员"), string.Format("添加管理员=={0}==成功", model.Mobile));
             if (EnterRepository.GetRepositoryEnter().SaveChange() > 0)
             {
                 return(Json(new
                 {
                     state = "success",
                     message = "添加管理员成功"
                 }));
             }
             else
             {
                 PublicFunction.AddOperation(1, string.Format("添加管理员"), string.Format("添加管理员=={0}==失败", model.Mobile));
                 EnterRepository.GetRepositoryEnter().SaveChange();
                 return(Json(new
                 {
                     state = "error",
                     message = "添加管理员失败"
                 }));
             }
         }
         else
         {
             return(Json(new
             {
                 state = "error",
                 message = "手机号码已经存在了"
             }));
         }
     }
     else
     {
         return(Json(new { state = "error", message = "信息不完整" }));
     }
 }
コード例 #2
0
        public JsonResult RestPassword(int id, string newPassword)
        {
            Random rn   = new Random();
            string salt = rn.Next(10000, 99999).ToString() + rn.Next(10000, 99999).ToString();

            EnterRepository.GetRepositoryEnter().GetAdminRepository.EditEntity(new Model.Admin()
            {
                Id = id, Password = MD5Helper.CreatePasswordMd5(newPassword, salt), Salt = salt
            }, new string[] { "Password", "Salt" });
            PublicFunction.AddOperation(1, string.Format("重置管理员密码"), string.Format("重置管理员=={0}==密码成功", id));
            if (EnterRepository.GetRepositoryEnter().SaveChange() > 0)
            {
                return(Json(new { state = "success", message = "重置管理员密码成功" }));
            }
            else
            {
                PublicFunction.AddOperation(1, string.Format("重置管理员密码"), string.Format("修改管理员=={0}==可登陆状态失败", id));
                EnterRepository.GetRepositoryEnter().SaveChange();
                return(Json(new { state = "error", message = "服务器泡妞去了" }));
            }
        }
コード例 #3
0
 public JsonResult Login(Models.LoginModel model)
 {
     if (ModelState.IsValid)
     {
         //首先判断下验证码是否正确
         if (Session["ValidateImgCode"] != null && string.Equals(Session["ValidateImgCode"].ToString(),
                                                                 model.ValidateCode, StringComparison.OrdinalIgnoreCase))
         {
             Model.Admin adminModel = new Model.Admin();
             if (new Regex("1[3|5|7|8|][0-9]{9}").IsMatch(model.UserName))  //匹配手机号码
             {
                 adminModel = EnterRepository.GetRepositoryEnter().GetAdminRepository.LoadEntities(m => m.Mobile == model.UserName && m.IsLogin == 1).FirstOrDefault();
             }
             else if (new Regex(@"[A-Za-z0-9.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}").IsMatch(model.UserName))  //匹配邮箱
             {
                 adminModel = EnterRepository.GetRepositoryEnter().GetAdminRepository.LoadEntities(m => m.Email == model.UserName && m.IsLogin == 1).FirstOrDefault();
             }
             else  //匹配用户名
             {
                 adminModel = EnterRepository.GetRepositoryEnter().GetAdminRepository.LoadEntities(m => m.AdminName == model.UserName && m.IsLogin == 1).FirstOrDefault();
             }
             if (adminModel == null)
             {
                 return(Json(new {
                     state = "error",
                     message = "用户名不存在"
                 }));
             }
             else
             {
                 //判断密码是否正确
                 if (adminModel.Password == MD5Helper.CreatePasswordMd5(model.Password, adminModel.Salt))
                 {
                     adminModel.LastLoginTime    = DateTime.Now;
                     adminModel.LastLoginIp      = IpHelper.GetRealIP();
                     adminModel.LastLoginAddress = IpHelper.GetAdrByIp(adminModel.LastLoginIp);
                     adminModel.LastLoginInfo    = IpHelper.GetBrowerVersion();
                     //添加登录日志并修改上次登录信息
                     EnterRepository.GetRepositoryEnter().GetAdminLoginLogRepository.AddEntity(new Model.AdminLoginLog()
                     {
                         AdminId           = adminModel.Id,
                         AdminLoginAddress = adminModel.LastLoginAddress,
                         AdminLoginIP      = adminModel.LastLoginIp,
                         AdminLoginTime    = adminModel.LastLoginTime,
                         AdminLoginInfo    = adminModel.LastLoginInfo
                     });
                     if (EnterRepository.GetRepositoryEnter().SaveChange() > 0)
                     {
                         //先清除原来的cookie
                         WebCookieHelper.AdminLoginOut();
                         //登录成功,保存cookie
                         WebCookieHelper.SetCookie(adminModel.Id, model.UserName, adminModel.LastLoginTime, adminModel.LastLoginIp, adminModel.LastLoginAddress, adminModel.IsSuperAdmin, adminModel.AuthoryId, (model.IsRemind != null && model.IsRemind)? 15 : 0);
                         return(Json(new {
                             state = "success",
                             message = "登录成功"
                         }));
                     }
                     else
                     {
                         return(Json(new {
                             state = "success",
                             message = "服务器泡妞去了"
                         }));
                     }
                 }
                 else
                 {
                     return(Json(new {
                         state = "error",
                         message = "密码错误"
                     }));
                 }
             }
         }
         else
         {
             return(Json(new {
                 state = "error",
                 message = "验证码错误"
             }));
         }
     }
     else
     {
         return(Json(new {
             state = "error",
             message = "输入信息不完整"
         }));
     }
 }
コード例 #4
0
        /// <summary>
        /// 员工登陆接口
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="loginSource">登陆来源</param>
        /// <returns></returns>
        public OperateResult Login(string userName, string pwd, byte loginSource)
        {
            OperateResult operateResult;

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(pwd))
            {
                operateResult = new OperateResult(OperateStateType.ParamError, "用户名或密码不能为空!");
                return(operateResult);
            }
            Emp empInfo;

            //先判断userName的类型
            if (Common.IsEmail(userName))  //第一步先判断是否是邮箱
            {
                empInfo = _empRepository.LoadEntities(m => m.FEmail == userName.Trim().ToLower() && !m.FIsDelete).FirstOrDefault();
            }
            else if (Common.IsMobile(userName))  //第二步先判断是否是手机号码
            {
                empInfo = _empRepository.LoadEntities(m => m.FMobile == userName.Trim() && !m.FIsDelete).FirstOrDefault();
            }
            else
            {
                empInfo = _empRepository.LoadEntities(m => m.FNo == userName.Trim() && !m.FIsDelete).FirstOrDefault();
            }
            if (empInfo == null)
            {
                operateResult = new OperateResult(OperateStateType.QueryNull, "该用户不存在!");
                return(operateResult);
            }
            else
            {
                string loginIp      = IpHelper.GetRealIP();
                string loginAddress = IpHelper.GetIpAddress(loginIp);
                string fLoginInfo   = IpHelper.GetBrowerVersion();
                //判断密码是否正确
                if (empInfo.FPwd.Equals(MD5Helper.CreatePasswordMd5(pwd, empInfo.FPwdSalt)))
                {
                    //判断是否为可登录
                    if (empInfo.FIsLimitLogin)
                    {
                        operateResult = new OperateResult(OperateStateType.Fail, "该用户被禁止登陆!");
                        return(operateResult);
                    }
                    else
                    {
                        operateResult = new OperateResult(OperateStateType.Success, empInfo);
                        //添加登陆信息,并修改登录历史纪录
                        LoginLog loginInfo = new LoginLog {
                            FIsSuccess    = true,
                            FLoginAddress = loginAddress,
                            FLoginInfo    = fLoginInfo,
                            FLoginIp      = loginIp,
                            FLoginSource  = loginSource,
                            FLoginTime    = DateTime.Now,
                            FUserId       = empInfo.FUserId,
                            FUserType     = (byte)UserType.员工
                        };
                        empInfo.FLastLoginSource = loginSource;
                        empInfo.FLastLoginTime   = DateTime.Now;
                        empInfo.FLastLoginDesc   = string.Format("登陆IP:{0},登陆地址:{1},登陆来源{2}:{3}", loginIp, loginAddress, loginSource.GetEnumName <Models.LoginSource>(), fLoginInfo);
                        if (_empRepository.UpdateLoginInfo(empInfo, loginInfo))
                        {
                            return(operateResult);
                        }
                        else
                        {
                            operateResult.OperateState = OperateStateType.Error;
                            operateResult.OData        = null;
                            operateResult.Message      = "登录失败!";
                            return(operateResult);
                        }
                    }
                }
                else
                {
                    operateResult = new OperateResult(OperateStateType.Fail, "用户名或密码错误!");
                    if (!empInfo.FIsLimitLogin)
                    {
                        //添加登陆信息,并修改登录历史纪录
                        LoginLog loginInfo = new LoginLog {
                            FIsSuccess    = false,
                            FLoginAddress = loginAddress,
                            FLoginInfo    = fLoginInfo,
                            FLoginIp      = loginIp,
                            FLoginSource  = loginSource,
                            FLoginTime    = DateTime.Now,
                            FUserId       = empInfo.FUserId,
                            FUserType     = (byte)UserType.员工
                        };
                        _loginLogRepository.AddEntity(loginInfo, true);
                        int errorCount = ConfigHelper.GetErrorCount();
                        int trueCount  = _loginLogRepository.LoadEntities(m => m.FUserId == empInfo.FUserId && !m.FIsSuccess && m.FLoginTime <= DateTime.Now.AddMinutes(-ConfigHelper.GetErrorTime())).Count();
                        if (errorCount > 0 && trueCount >= errorCount)
                        {
                            //将该员工设置为不可登录
                            empInfo.FIsLimitLogin = true;
                            EmpOperateLog operateLogInfo = new EmpOperateLog {
                                FActionType     = 10,
                                FDesc           = "由于输入密码错误次数过多,将此账号设置为禁止登陆",
                                FKeyId          = empInfo.FUserId,
                                FOperateAddress = loginAddress,
                                FOperateIp      = loginIp,
                                FOperateSource  = loginSource,
                                FOperateTime    = DateTime.Now,
                                FOperateUserId  = empInfo.FUserId,
                                FSourceInfo     = fLoginInfo,
                                FState          = false,
                                FTitle          = "多次登陆失败"
                            };
                            _empRepository.UpdateLimitLogin(empInfo, operateLogInfo);
                        }
                    }
                    return(operateResult);
                }
            }
        }