Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:08:31+00:00 2026-05-26T14:08:31+00:00

In ASP.NET MVC, I would like to somehow do custom authorization using the MvcSiteMapProvider.

  • 0

In ASP.NET MVC, I would like to somehow do custom authorization using the MvcSiteMapProvider.

I am aware I can implement a custom Authorization Attribute that inherits from AuthorizeAttribute. Then, we can perhaps decorate out controllers with [SiteMapAuthorize] for instance.

Is this the best route? If so, what I am looking for is the proper implementation of using a site map provider with authorize.

public class SiteMapAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {

    }
}

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:08:31+00:00Added an answer on May 26, 2026 at 2:08 pm

    I’ve got this working

    Here is my solution:

    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;
        }
    }
    

    The HandleUnauthorizedRequest works with the customErrors section in the web.config:

    <customErrors mode="On" defaultRedirect="~/Error">
      <error statusCode="404" redirect="~/Error/NotFound"/>
      <error statusCode="403" redirect="~/Error/AccessDenied"/>
    </customErrors>
    

    You will need an Errors Controller for the above customErrors to work:
    How to use CustomErrors in ASP.NET MVC 2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC Preview 4 and would like to know how to use
Using log4net we would like to log all calls to our ASP.NET MVC controller
We have an asp.net mvc application that we would like to integrate a FileSystemWatcher
I am using the Html.DropDownList helper in ASP.NET MVC and I would like to
I've created a few html helper methods for asp.net mvc that I would like
I'm using ASP.NET MVC and would like to get Model information for use within
I'm using ASP.NET MVC 3 and ASP.NET MVC Sitemap Provider . I would like
In my asp.net MVC 3 project I would like to create a contact that's
I'm using ASP.NET MVC 3 with unobstrusive ajax and I would like to prevent
Im developing an asp.net mvc 2 project and I would like to implement the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.