/// <summary> /// 改写onactionexecuting(在controller action执行之前调用),去判断请求中是不是存了session。使用场景:如何验证登录等。 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!SmartEyeLoginSession.IsLogin()) { filterContext.HttpContext.Response.Redirect((new UrlHelper(filterContext.RequestContext)).Action("Login", "UserLogin")); //HttpContext.Current.Response.Write("<script>alert('请先登录');window.parent.location.href='/Users/Login'</script>"); } //这种是通过返回一段js代码来实现跳转登录页面 //if (filterContext.HttpContext.Session["UserName"] == null) //{ //filterContext.HttpContext.Response.Redirect((new UrlHelper()).Action("Login", "Admin")); //}//这种就是直接通过过滤器上下文的的http上下文请求来进行重置链接 }
public ActionResult DoLogin(UserModel model) { try { string msg = string.Empty; bool falg = false; string sql = string.Format(" SELECT UserName FROM TAdminUser WHERE UserName=@UserName AND PASSWORD=@PASSWORD "); List <SqlParameter> para = new List <SqlParameter>(); para.Add(new SqlParameter("@UserName", model.username)); para.Add(new SqlParameter("@PASSWORD", model.password)); DataSet ds = SQLHelper.Instance.ExecuteDataSet(sql, para.ToArray()); if (ds != null && ds.Tables != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0) { SmartEyeLoginSession.SetSmartSession(new LoginInfoModel(ds.Tables[0].Rows[0]["UserName"].ToString()), 1); falg = true; msg = "登录成功"; } else { msg = "登录失败,请输入正确的账号信息"; } return(Json(new { status = falg ? "success" : "", message = msg })); } catch (Exception ex) { return(Json(new { status = "false", message = ex.Message })); } }