2013-02-11 98 views
0

我有一个与动作重定向有关的小问题。我想阻止用户使用我的BaseController类中的OnActionExecuting类来访问站点中特定区域的信息,这是我所有控制器的基类。方法体:MVC 3重定向不起作用

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (Request.IsAuthenticated && (User as Eagle.Security.EaglePrincipal != null) && Session != null && Session["LastKnownGoodArea"] != null && filterContext.ActionDescriptor.ActionName != "InvalidPermission") 
     { 
      var currentArea = Principal.CurrentCenter.CODEFORM_CSE; 
      if (currentArea != Session["LastKnownGoodArea"].ToString()) 
       RedirectToActionPermanent("InvalidPermission", "Account", new { target = 0, redirectURL = null as string }); 
      else 
       base.OnActionExecuting(filterContext); 
     } 

    } 

但是,这不会重定向到指定的操作。我究竟做错了什么?如果有其他方法,你们会建议吗?

感谢, 的Silviu

+0

请看这里:http://stackoverflow.com/questions/4909670/asp-net-mvc-3-redirect-to-another-action – Pandian 2013-02-11 13:27:02

+0

嗯,这不是一个真正的选择,因为在某些情况下,我只是想调用基类(Controller)OnActionExecuting,它具有void作为返回类型,而这正是它应该如何工作的。 – 2013-02-11 13:31:36

回答

1

什么戴夫评论是正确的!除了这应该是语法您正在寻找: -

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      if (Request.IsAuthenticated && (User as Eagle.Security.EaglePrincipal != null) && Session != null && Session["LastKnownGoodArea"] != null && filterContext.ActionDescriptor.ActionName != "InvalidPermission") 
      { 
       var currentArea = Principal.CurrentCenter.CODEFORM_CSE; 
       if (currentArea != Session["LastKnownGoodArea"].ToString()) 
       { 
       filterContext.Result = new RedirectToRouteResult(new 
        RouteValueDictionary(new 
        { 
         controller = "InvalidPermission", 
         action = "Account", 
         target = 0,           
        })); 
       filterContext.Result.ExecuteResult(filterContext); 
       }  
       else 
       { 
        base.OnActionExecuting(filterContext); 
       } 
      } 

     } 
+0

那么,从您的两个建议合并,以及您真正的最后一步,我做到了! – 2013-02-11 14:21:20

+0

+1执行得好 – 2013-02-11 15:20:32

0

,因为它没有创建一个动作结果却无法从过滤器重定向到行动。您只能创建一条新路线。我不完全确定你需要的语法。我一起把它作为一个例子。

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (Request.IsAuthenticated && (User as Eagle.Security.EaglePrincipal != null) && Session != null && Session["LastKnownGoodArea"] != null && filterContext.ActionDescriptor.ActionName != "InvalidPermission") 
     { 
      var currentArea = Principal.CurrentCenter.CODEFORM_CSE; 
      if (currentArea != Session["LastKnownGoodArea"].ToString()) 
      filterContext.Result = new RedirectToRouteResult(
    new System.Web.Routing.RouteValueDictionary { 
     {"controller", "InvalidPermission"}, {"action", "Account"}, {target =0}, {redirectURL = null as string } 

      else 
       base.OnActionExecuting(filterContext); 
     } 

    } 
1

我想,以防止用户能够访问使用OnActionExecuting的覆盖在我的BaseController关于该网站的特定区域信息类,这是我所有控制器的基类。

为什么选择使用OnActionExecuting?你在每次请求执行这个if语句,我会建议使用Authorize属性的具体行动:

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var user = User as Eagle.Security.EaglePrincipal; 

     if(httpContext.User.Identity.IsAuthenticated && user != null) 
     { 
      var currentArea = Principal.CurrentCenter.CODEFORM_CSE; 
      var lastKnownArea = Session["LastKnownGoodArea"]; 

      if (lastKnowArea == null) 
       return false; 

      return currentArea.Equals(lastKnownArea.ToString()); 
     } 

     return base.AuthorizeCore(httpContext);    
    } 
} 

在你web.config您可以配置重定向,如:

<customErrors mode="RemoteOnly"> 
    <error statusCode="403" redirect="/InvalidPermission/Account" /> 
</customErrors> 

如果您想要控制未经授权的请求,您可以随时选择覆盖HandleUnauthorizedRequest方法

+0

嗯,这将是一个很好的解决方案,我给你,但这意味着我将不得不添加新的attr到我所有的方法,我真的不喜欢它。但我会保留这个作为未来的参考。谢谢。 – 2013-02-11 14:24:37

+0

好的,如果所有的方法都在同一个控制器中,您可以随时将该属性分配给控制器。 – 2013-02-11 14:27:03

+0

当前的体系结构具有跨几个(读取十个,可能接近100个)控制器的方法。但!!!它们都从BaseController继承。让我看看我能否以某种方式在这堂课中实施你的想法,并看看它是否会沿着这条链条传播下去。如果是这样,这将看起来很精致,我将在这件事上跟随你的领导。将回来的测试结果;) – 2013-02-11 14:31:33

1

以下是最终解决方案:

var currentArea = Principal.CurrentCenter.CODEFORM_CSE; 
      if (currentArea != Session["LastKnownGoodArea"].ToString()) 
      { 
       filterContext.Result = new RedirectToRouteResult(new 
        RouteValueDictionary(new 
        { 
         controller = "Account", 
         action = "InvalidPermission", 
         area = "", 
         target = 0, 
         redirectURL = "" 
        })); 
      } 
      else 
      { 
       base.OnActionExecuting(filterContext); 
      } 

谢谢你们俩的意见,你们帮助过很多! 干杯!

+0

+1做得好,欢迎来到SO – 2013-02-11 15:30:52