/// <summary> /// 在Action之前调用 /// tip:主要来验证用户登录 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { //是否需要验证登录 bool needLogin = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoLogin), true).Length == 1 ? false : true; if (needLogin) { //验证登录 bool IsAjax = filterContext.HttpContext.Request.IsAjaxRequest(); if (Session[sUserSessionKey] != null) { LoginStatus = (LoginCacheInfo)Session[sUserSessionKey]; } else { if (IsAjax && filterContext.HttpContext.Request.HttpMethod.ToUpper() == "POST") { result.over = true; filterContext.Result = Json(result); } else { //登录失效跳转登录 filterContext.Result = Redirect("/User/Login"); } } } }
public ActionResult Login(string sUserName, string sPassword, string sCode) { if (!Request.IsAjaxRequest()) { return(View()); } else { if (string.IsNullOrEmpty(sCode)) { result.info = "参数错误"; return(Json(result)); } if (sCode != Convert.ToString(Session[sCodeSessionKey])) { result.info = "验证码错误"; return(Json(result)); } sPassword = SecurityHelper.MD5(sPassword); string Ip = Request.UserHostAddress; var user = mangae.db.TG_User.SingleOrDefault(m => m.sUserName == sUserName && m.sPassword == sPassword); if (user != null) {//登录成功 var now = DateTime.Now; var log = mangae.db.TG_LoginLog.Where(m => m.sUserName == sUserName). OrderByDescending(m => m.dInsertTime).FirstOrDefault(); LoginStatus = new LoginCacheInfo(); LoginStatus.ID = user.ID; LoginStatus.sUserName = user.sUserName; LoginStatus.iUserType = user.iUserType; LoginStatus.sCategoryIds = user.sCategoryIds; if (log != null) { LoginStatus.dLoginTime = log.dInsertTime; LoginStatus.Ip = log.Ip; LoginStatus.isFirst = false; } else { LoginStatus.dLoginTime = now; LoginStatus.Ip = Ip; LoginStatus.isFirst = true; } Session[sUserSessionKey] = LoginStatus; result.success = true; result.info = "登录成功"; result.data = LoginStatus.iUserType; Task.Factory.StartNew(() => { InsertLog(sUserName, Ip, now); }); } else {//登录失败 result.info = "用户名或密码错误!"; } Session[sCodeSessionKey] = null; return(Json(result)); } }