2014-10-29 148 views
6

好的。所以我有一個問題,我需要在控制器操作中進行一些授權檢查。ASP.Net MVC從控制器的局部視圖重定向到不同控制器的全視圖

有授權的角色,但它可能存在一些別人TypeOnePayment,但不TypeTwo

[Authorize(Roles = "TypeOnePayment;TypeTwoPayment")] 
public ActionResult EnterRevenue(PaymentType payment) 
{ 
    payment = "TypeOne"; // This exists for show only. 
    var permission = string.Concat(payment,"Permission"); 
    if (!SecurityUtility.HasPermission(permission)) 
    { 
     return View("Unauthorized", "Error"); 
    } 
    return this.PartialView("_EnterRevenue"); 
} 

但由於這是返回的局部視圖,「錯誤」屏幕只出現在局部視圖部分這一頁。有沒有辦法重定向到一個全新的頁面?

編輯:EnterRevenue通過ajax調用正在檢索。所以只是html被返回,它被放置在它被調用的視圖中。

+0

你在哪裏使用這個局部視圖?在ajax或Html.Action? – 2014-10-29 18:59:07

+0

它使用ajax調用進行檢索,並在調用成功時將新的html插入到html中。 – ELepolt 2014-10-29 19:07:14

+0

你應該讀這:http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – 2014-10-29 19:28:11

回答

5

您可以重定向到一些其他的動作:

public ActionResult EnterRevenue 
{ 
    if (!SecurityUtility.HasPermission(permission)) 
    { 
     return View("Unauthorized", "Error"); 
    } 
    return RedirectToAction("NotAuthorized","Error"); 
} 

假設我們有ErrorController用行動NotAuthorized返回正常視圖,其中顯示您無權查看此頁。

如果你需要檢查每個動作,那麼你需要實現自定義動作過濾器屬性,你必須檢查它是否是正常的請求重定向,否則返回staus作爲json並從客戶端重定向。見asp.net mvc check if user is authorized before accessing page

這裏是一個代碼塊:

public class AuthorizationAttribute : ActionFilterAttribute 
    { 
     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      string actionName = filterContext.ActionDescriptor.ActionName; 
      string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; 


      if (filterContext != null) 
      { 
       HttpSessionStateBase objHttpSessionStateBase = filterContext.HttpContext.Session; 
       var userSession = objHttpSessionStateBase["userId"]; 
       if (((userSession == null) && (!objHttpSessionStateBase.IsNewSession)) || (objHttpSessionStateBase.IsNewSession)) 
       { 
        objHttpSessionStateBase.RemoveAll(); 
        objHttpSessionStateBase.Clear(); 
        objHttpSessionStateBase.Abandon(); 
        if (filterContext.HttpContext.Request.IsAjaxRequest()) 
        { 
         filterContext.HttpContext.Response.StatusCode = 403; 
         filterContext.Result = new JsonResult { Data = "LogOut" }; 
        } 
        else 
        { 
         filterContext.Result = new RedirectResult("~/Home/Index"); 
        } 

       } 


       else 
       { 

        if (!CheckAccessRight(actionName, controllerName)) 
        { 
         string redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery); 

         filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true); 
        } 
        else 
        { 
         base.OnActionExecuting(filterContext); 
        } 
       } 


      } 

     } 
} 

,並用它的行動是這樣的:

[Authorization] 
public ActionResult EnterRevenue 
{ 
    return this.PartialView("_EnterRevenue"); 
} 
+0

我可能已經措辭這一點很差。但EnterRevenue是局部視圖。因此,不管返回的是什麼,它只會顯示在被調用的主視圖的局部視圖空間中。 – ELepolt 2014-10-29 19:01:15

+0

你怎麼打電話給EventRevenue行動?顯示那段代碼? – 2014-10-29 19:02:48

+0

編輯我的帖子,但EnterRevenue正在通過ajax調用檢索。所以只是html被返回,它被放置在它被調用的視圖中。 – ELepolt 2014-10-29 19:09:04

0

我想你需要什麼都可以歸結爲一種爲AJAX根據你要返回的行爲調用行爲不同。我發現這樣做的最好結果如下:

  • 當您檢測到您沒有權限時,向模型添加模型狀態錯誤。
  • 重寫OnActionExecuted(希望你的所有控制器都繼承自一個基本的控制器,這樣你就可以在一個地方完成它,如果沒有的話,現在實現它可能是一個好主意)。在覆蓋中,檢查請求是否爲ajax,模型狀態無效(如果您希望您可以檢查您在操作方法中添加的特定錯誤),請將請求狀態更改爲4xx狀態。
  • 在您的ajax調用的OnFailure中,您可以使用JavaScript代碼重定向到錯誤頁面。
0

或者只是使用標準的重定向呼叫。這應該在任何地方工作(只是不這樣做內using聲明的或將拋在後臺的除外):

Response.Redirect("/Account/Login?reason=NotAuthorised", true);       
相關問題