Esempio n. 1
0
 public ActionResult LoginOut()
 {
     if (WebCookieHelper.AdminCheckLogin())
     {
         WebCookieHelper.AdminLoginOut();
     }
     return(RedirectToAction("Login", "Account"));
 }
 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 = "输入信息不完整"
         }));
     }
 }