shuai_wy
超帅的菜鸟博主
2017-10-13 01:48

ASP.NET MVC 身份验证失败后,返回JSON数据

2
  • asp.net
  • 身份认证
  • mvc

正常的逻辑失败后,应该是跳转 登录页,但是: 异步请求的时候,在登录过期后,返回一个登录页,JS是识别不了的,我想返回JSON数据,应该怎样实现?

  public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
            {
                // filterContext.Result = new RedirectResult("/account/login");
                    FormsAuthentication.RedirectToLoginPage();//重定向会登录页
            }
            else
            {
                //登录状态获取用户信息
                var cookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                AdminModel loginUser = new AdminModel();
                loginUser = new JavaScriptSerializer().Deserialize<AdminModel>(ticket.UserData);

                filterContext.Controller.ViewData["UserName"] = loginUser.UserName;
                filterContext.Controller.ViewData["Portrait"] = loginUser.Portrait;
                filterContext.Controller.ViewData["UserID"] = loginUser.ID;
            }
            base.OnActionExecuting(filterContext);
        }
    }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答