2013-02-11 94 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