我有這方面的工作
這裏是我的解決方案:
public class SiteMapAuthorizeAttribute : AuthorizeAttribute
{
public string Action { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (!httpContext.User.Identity.IsAuthenticated)
return false;
var node = SiteMap.CurrentNode;
// If the node is null, then it was not loaded into memory
// because this user was not authorized to view this node
if (node == null)
return false;
// Check the node's accessibility regardless in case we got passed the above check
return node.IsAccessibleToUser(HttpContext.Current);
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
// If user is not authenticated allow default handling
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
return;
}
string customErrorPage = GetCustomError("403");
if (customErrorPage == null)
{
base.HandleUnauthorizedRequest(filterContext);
return;
}
// Redirect to 403 (Access Denied) page
filterContext.Result = new RedirectResult(customErrorPage);
}
private string GetCustomError(string statusCode)
{
CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
if (customErrorsSection != null)
{
CustomError customErrorPage = customErrorsSection.Errors[statusCode];
if (customErrorPage != null)
return customErrorPage.Redirect;
}
return null;
}
}
的HandleUnauthorizedRequest與工作在web.config中的customErrors部分:
<customErrors mode="On" defaultRedirect="~/Error">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="403" redirect="~/Error/AccessDenied"/>
</customErrors>
您將需要一個Errors Controller for the custom customErrors work: How to use CustomErrors in ASP.NET MVC 2