2014-10-29 154 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);       
相关问题