2012-03-27 111 views
0

我的應用程序派生自System.Web.Mvc.Controller基類中有一個基礎控制器類。
在重寫方法「ActionResultExecuting」我有我的業務邏輯,檢查存在的身份驗證cookie。
如果當前請求沒有身份驗證Cookie /滿足我的業務條件,我需要將請求重定向到註銷操作,然後重定向到登錄操作。控制器動作從派生控制器類重定向

請找我的代碼片段

public class MyBaseController : Controller 
{ 
    protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     ///... User story 1 - Domain1 Cookie present and Domain2 cookie present - Session ID references match in both the cookies 
     HttpCookie Domain1Cookie= filterContext.HttpContext.Request.Cookies["dm1"]; 
     HttpCookie Domain2Cookie= filterContext.HttpContext.Request.Cookies["dm2"]; 

     if (Domain1Cookie != null && Domain2Cookie != null) 
     { 
      string eacCookieValue = Domain1Cookie.Value; 
      string enrollmentCookieValue = Domain2Cookie.Value; 
      if (eacCookieValue.Contains(enrollmentCookieValue)) 
      { 
       string controllerName = RouteData.Values["Controller"].ToString(); 
       string actionName = RouteData.Values["Action"].ToString(); 

       if (controllerName != "Account" && actionName != "Login") 
       { 
        ////... This is where i need to put my redirection code... Redirect the User to LogOut Action and then redirect to Login 
       } 

      } 
     } 
     base.OnActionExecuting(filterContext); 
    }  
} 

在此先感謝!

回答

0

您可以將filterContext.Result屬性設置爲RedirectToRouteResult,表示您願意重定向的控制器和操作。你可以明顯地傳遞任何額外的路由值和查詢字符串參數你喜歡:

if (controllerName != "Account" && actionName != "Login") 
{ 
    ////... This is where i need to put my redirection code... Redirect the User to LogOut Action and then redirect to Login 
    var values = new RouteValueDictionary(new 
    { 
     controller = "Account", 
     action = "LogOut" 
    }); 
    filterContext.Result = new RedirectToRouteResult(values); 
} 

不過要達到這樣的比較正確的做法是重寫OnAuthorization方法,而不是OnActionExecuting

相關問題