【问题标题】:ASP.NET MVC Custom Action Filter is not redirectingASP.NET MVC 自定义操作筛选器未重定向
【发布时间】:2014-06-30 07:15:56
【问题描述】:

我正在尝试使用如下操作过滤器将会话过期的用户重定向到登录页面:

public class SessaoFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string controllerName = filterContext.Controller.GetType().Name;
            string actionName = filterContext.ActionDescriptor.ActionName;
            if (filterContext.HttpContext.Session != null)
            {
                if (filterContext.HttpContext.Session["Autenticado"] == null)
                {
                  if (!controllerName.Equals(typeof(LoginController).Name, StringComparison.InvariantCultureIgnoreCase)
                  || (!actionName.Equals("Login", StringComparison.InvariantCultureIgnoreCase) 
                  && !actionName.Equals("Autenticar", StringComparison.InvariantCultureIgnoreCase)))
                    {
                        filterContext.Result = 
                          new RedirectToRouteResult(
                             new RouteValueDictionary{
                                     { "controller", "Login" },
                                     { "action", "Login" }
                             });
                    }
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }

当 base.OnActionExecuting(filterContext) 被处理时,firefox 浏览器只收到这些答案,而不是重定向到登录页面:

GET (address)/Login/OpenChangePassword?_=1399920034730 200 OK 140ms  jquery-1.7.1.js (line 8102) --> **The action that a Tried to call**

GET (address)/Scripts/jquery-1.7.1.js?_=1399920034951  200 OK 8ms        jquery-1.7.1.js (line 8102)

GET (address)/maskedinput-1.1.2.pack.js?_=1399920035043 200 OK 3ms  jquery-1.7.1.js (line 8102)

Password fields present on an insecure (http://) page. This is a security risk that allows user login credentials to be stolen. 0

Password fields present in a form with an insecure (http://) form action. This is a security risk that allows user login credentials to be stolen. 0

GET (address)/Scripts/jquery.dataTables.js?_=1399920035078 200 OK 7ms   jquery-1.7.1.js (line 8102) 

GET (address)/Scripts/select2.js?_=1399920035155 200 OK 14ms    jquery-1.7.1.js (line 8102)

GET (address)/Scripts/select2_locale_pt-BR.js?_=1399920035213 200 OK 2ms jquery-1.7.1.js (line 8102)

GET (link)/Scripts/jquery-ui-1.8.20.js?_=1399920035238 200 OK 6ms jquery-1.7.1.js (line 8102)

GET (address)/Scripts/jquery.unobtrusive-ajax.js?_=1399920035339 200 OK 4ms jquery-1.7.1.js (line 8102)

GET (address)/Scripts/jquery.validate.js?_=1399920035368 200 OK 4ms jquery-1.7.1.js (line 8102)

GET (link)/Scripts/jquery.validate.unobtrusive.js?_=1399920035399 200 OK 2ms

这些.js 文件是我渲染到登录页面的文件。

有人可以帮忙吗?

Tks

【问题讨论】:

    标签: asp.net-mvc http-redirect action-filter custom-action-filter


    【解决方案1】:

    对于这种情况,我建议使用派生自 AuthorizeAttribute 的过滤器,然后覆盖 AuthorizeCore 方法。据我所知,这类过滤器在任何其他过滤器之前执行(包括那些从 ActionFilterAttribute 派生的过滤器,比如你的过滤器)。

    我有一个和你类似的场景,并且派生自 AuthorizeAttribute 为我完成了这项工作。

    【讨论】:

    • 谢谢@Matthias。 AuthorizeAttribute 真的很好用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2019-07-29
    • 2011-05-29
    相关资源
    最近更新 更多