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 6785181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:05:46+00:00 2026-05-26T17:05:46+00:00

I have an secure application with an Authorize attribute on each action. [Authorize(Roles =

  • 0

I have an secure application with an Authorize attribute on each action.

[Authorize(Roles = "Role1,Role2")]
public ActionResult MyAction(int id)
{
    return View();
}

In my UI, I have links to these controller/actions. I would like to create a custom HtmlHelper for the links that accepts controller and action names:

@Html.SecuredLink("Click Me", "MyAction", "MyController");

And this would determine weather to render itself or not based on if the user has permission to the given action:

public static MvcHtmlString SecuredLink(this HtmlHelper helper, string text, string action, string controller)
{        
    var userId = Membership.GetUserId();

    var userHasRightsToThisAction = IsActionAccessibleToUser(helper.ViewContext.RequestContext.HttpContext, controller, action); // <- How would this work?

    if (userHasRightsToThisAction )
    {
       // Render Link
       // ...
    }
}

I have been unable to find a way to easily test the action from code for authorization status.

  • 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-26T17:05:46+00:00Added an answer on May 26, 2026 at 5:05 pm

    Ok found a solution. After digging around the MvcSiteMap which I know does security trimming, I found this article about it:

    http://blog.maartenballiauw.be/post/2008/08/29/Building-an-ASPNET-MVC-sitemap-provider-with-security-trimming.aspx

    I used a bit of this code, modified slightly, to create the method that gives me the desired result:

        /// <summary>
        /// Determine if a controller/action is accessible for a user
        /// </summary>
        /// <param name="context">Current HttpContext</param>
        /// <param name="controllerName">Target controller</param>
        /// <param name="actionName">Target action</param>
        /// <returns>True/false if the action is accessible</returns>
        public static bool IsActionAccessibleToUser(HttpContextBase context, string controllerName, string actionName)
        {
            // Find current handler
            MvcHandler handler = context.Handler as MvcHandler;
    
            if (handler != null)
            {
                // try to figure out the controller class
                IController controller = null;
                try
                {
                    controller = ControllerBuilder.Current.GetControllerFactory().CreateController(handler.RequestContext, controllerName);                    
                }
                catch (System.Web.HttpException e)
                {
                    throw new Exception("The controller '" + controllerName + "Controller' was not found.", e);
                }
    
                // Find all AuthorizeAttributes on the controller class and action method
                object[] controllerAttributes = controller.GetType().GetCustomAttributes(typeof(AuthorizeAttribute), true);
                object[] actionAttributes = controller.GetType().GetMethod(actionName).GetCustomAttributes(typeof(AuthorizeAttribute), true);
    
                // No attributes, then the action is open to all
                if (controllerAttributes.Length == 0 && actionAttributes.Length == 0) return true;
    
                // Find out current principal
                IPrincipal principal = handler.RequestContext.HttpContext.User;
    
                // Do we pass the roles for the controller?
                string roles = "";
                if (controllerAttributes.Length > 0)
                {
                    AuthorizeAttribute attribute = controllerAttributes[0] as AuthorizeAttribute;
                    roles = attribute.Roles;
    
                    if (!PassRoleValidation(principal, roles)) return false;
                }
    
                // Do we pass the roles for the action?
                if (actionAttributes.Length > 0)
                {
                    AuthorizeAttribute attribute = actionAttributes[0] as AuthorizeAttribute;
                    roles = attribute.Roles;
    
                    if (!PassRoleValidation(principal, roles)) return false;
                }
    
                return true;
            }
    
            return false;
        }
    
        private static bool PassRoleValidation(IPrincipal principal, string roles)
        {
            // no roles, then all we need to be is authenticated
            if (string.IsNullOrEmpty(roles) && principal.Identity.IsAuthenticated) return true;
    
            string[] roleArray = roles.Split(',');
    
            // if role contains "*", it's open to all
            if (roleArray.Any(role => role == "*")) return true;
    
            // Determine if the current user is allowed to access the current node
            if (roleArray.Any(principal.IsInRole)) return true;
    
            return false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application which contains both secure (SSL) and non-secure pages. A
I have a small AJAX application, written in PHP that I did not secure
Currently I use [Authorize(Roles = .....)] to secure my controller actions on my ASP.NET
I have a clean GWT application which I want to secure with Spring Security.
I'm making a web application and would like to have a secure area where
I have written a secure TCP server in .NET. This was basically as simple
I have users who are using secure-wave security. Evidently it is some sort of
I have a website which allows secure (ssl) file uploads and download. The site
We have been doing some research into physically isolating the secure and non-secure sections
I have a jQuery script: $.ajax({ url: /scripts/secure/development/ajax.asp, type: POST, dataType: text, data: cmd=addresses,

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.