我试图实现一个 AuthorizeAttribute 以便我可以将其应用于某些子操作,其目的是例如在该人被授权并有权访问时显示菜单,否则返回没有错误或重定向到登录页面。我尝试了几种不同的退货选项,但都没有成功。下面的例子
//SideMenu.cshtml
@{Html.RenderAction("AdminMenu", "Menu", "Menu");}
@{Html.RenderAction("CommitteeMenu", "Menu", "Menu");}
@{Html.RenderAction("MemberMenu", "Menu", "Menu");}
@{Html.RenderAction("PublicMenu", "Menu", "Menu");}
//MenuController
[BlockChildContentAuthorize(Roles = " Committee, Admin, Full_rights")]
[ChildActionOnly]
public virtual ActionResult CommitteeMenu()
{
....
}
//Authorize.cs
public class BlockChildContentAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.IsChildAction)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
}
else
{
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(HttpStatusCode.Unauthorized);
return;
}
}
}
}