2012-03-27 114 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

相关问题