【问题标题】:Handling Security in Asp.net MVC pages with ActionFilters and sessions使用 ActionFilters 和会话处理 Asp.net MVC 页面中的安全性
【发布时间】:2014-01-24 23:40:59
【问题描述】:

我需要在用户会话过期并重定向到登录页面时显示警报。 实际上我已经创建了一个应用在控制器上的动作过滤器并检查 如果会话过期,如果 es 重定向到登录页面,否则它让操作完成 成功。

它工作正常,但是当我发出 ajax 请求时出现了我的问题。 例如,我的视图中有 ajax.beginform,并且我已经在该操作上保存了代码 方法。现在,假设用户在会话过期时提交了表单, 在保存方法运行之前,我的 actionFilter 被调用,因为我已将其设置为 控制器,所以,从那里它说,重定向到登录页面。 但我从控制器返回 Json,它不会重定向。

我应该如何实现这些东西?

我的代码是::::::::::::

对于每个控制器,我的动作过滤器以相同的方式定义,我只为一个控制器定义所以,假设下面是我的控制器。

[CheckSessions]
    public class MyController : Controller
    {
             public ActionResult Add(Model model)
        {
                 Insert code and returns 
                return Json(new { Msg = Message});
         }
    }

Action Filter is here
   public class CheckSessions: ActionFilterAttribute
    { 
public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
if (HttpContext.Current.Session["LoginSession"] == null && HttpContext.Current.Session["LoginSession"] == null)
            {
                HttpContext.Current.Response.Redirect("/LoginController/Login");
            }
}
}


In View Page:::
   @using (Ajax.BeginForm("Add", "MyController ", new AjaxOptions { OnSuccess = "MessageConfirmation" }))
            {
   Content goes here
}

<script>
Getting Response

    function MessageConfirmation(Json) {
        alert(Json.StatusCode);
if(Json.StatusCode==404)
{
 redirect to login
}
}
</script>

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    您无法为 Ajax 请求执行 Response.Redirect

    而不是重定向,您发送表示会话过期的响应文本/代码,并使用window.location.href进行重定向

     public class CheckSessions: ActionFilterAttribute
     { 
       public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
          if (HttpContext.Current.Session["LoginSession"] == null && 
           HttpContext.Current.Session["LoginSession"] == null)
          {
               if(HttpContext.Current.Request.IsAjaxRequest())
                {
                 HttpContext.Current.Response.StatusCode="401"; //Session Expired
                }
               else
                {
                 HttpContext.Current.Response.Redirect("/Account/Login");
                }
    
           }
        }
      }
    

    在 Javascript/jQuery 中检查状态码 ==="401"

    选项 1:

    新的 AjaxOptions { OnSuccess = "MessageConfirmation(data, status, xhr)" }

    函数消息确认(数据,状态,xhr) {

     //check the status code from xhr
     window.location.href="/Account/Login";
    

    }

    选项 2: 全局执行

    $.ajax({
      statusCode: {
        401: function(xhr) {
          window.location.href="/Account/Login";
        }
      }
    

    });

    【讨论】:

    • 但是我将如何通过 json 发送状态?请提出您的建议。
    • @Sweetie,如果是 JSON 请求,只需设置 HttpContext.Current.Response.ContentType ="application/json"
    • @Sweetie,不需要设置内容类型,因为你处理的是请求状态,所有jQuery ajax请求都可以使用,包括GET、POST、JSONP等
    • 假设通过这一行我设置了状态代码..... HttpContext.Current.Response.StatusCode="401";现在,当我在 Ajax 的成功方法中获得响应时,我将如何检查 Java 脚本,开始表单
    • 只需使用状态代码并检查 ajax 调用返回的响应代码。如果你使用 jquery
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2013-08-06
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多