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

  • Home
  • SEARCH
  • 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 768077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:07:26+00:00 2026-05-14T18:07:26+00:00

How can I create a security aware action link that detects if a user

  • 0

How can I create a “security aware” action link that detects if a user is authorized to click (invoke) the action?
Hide link if user is not allowed to use that action…

Depending from

  • web.config (authorization) and
  • [Authorize] attributes on actions

PS
I guess it is bad practice to mix those 2 in MVC?

  • 1 1 Answer
  • 1 View
  • 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-14T18:07:27+00:00Added an answer on May 14, 2026 at 6:07 pm

    This is some code poached from the MvcSitemap project and modified for my own use. If I remember correctly this code has been modified for MVC2 and some of the functions might have to be back ported to MVC1.

    Its not bad practices at all to mix MVC and FormsAuthentication together, MVC’s default authentication methods are build around the existing Asp.net security infrastructure.

    Code to determine if user has permissions:

    public static class SecurityTrimmingExtensions 
    {
    
        public static bool HasActionPermission( this HtmlHelper htmlHelper, string actionName, string controllerName )
        {
            //if the controller name is empty the ASP.NET convention is:
            //"we are linking to a different controller
            ControllerBase controllerToLinkTo = string.IsNullOrEmpty(controllerName) 
                                                    ? htmlHelper.ViewContext.Controller
                                                    : GetControllerByName(htmlHelper, controllerName);
    
            var controllerContext = new ControllerContext(htmlHelper.ViewContext.RequestContext, controllerToLinkTo);
    
            var controllerDescriptor = new ReflectedControllerDescriptor(controllerToLinkTo.GetType());
    
            var actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);
    
            return ActionIsAuthorized(controllerContext, actionDescriptor);
        }
    
    
        private static bool ActionIsAuthorized(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if (actionDescriptor == null)
                return false; // action does not exist so say yes - should we authorise this?!
    
            AuthorizationContext authContext = new AuthorizationContext(controllerContext);
    
            // run each auth filter until on fails
            // performance could be improved by some caching
            foreach (IAuthorizationFilter authFilter in actionDescriptor.GetFilters().AuthorizationFilters)
            {
                authFilter.OnAuthorization(authContext);
    
                if (authContext.Result != null)
                    return false;
            }
    
            return true;
        }
    
        private static ControllerBase GetControllerByName(HtmlHelper helper, string controllerName)
        {
            // Instantiate the controller and call Execute
            IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
    
            IController controller = factory.CreateController(helper.ViewContext.RequestContext, controllerName);
    
            if (controller == null)
            {
                throw new InvalidOperationException(
    
                    String.Format(
                        CultureInfo.CurrentUICulture,
                        "Controller factory {0} controller {1} returned null",
                        factory.GetType(),
                        controllerName));
    
            }
    
            return (ControllerBase)controller;
        }
    
    }
    

    Html Helpers

    public static class SecurityTrimmedLink
    {
        public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName)
        {
            return htmlHelper.HasActionPermission(actionName, "")
                       ? htmlHelper.ActionLink(linkName, actionName)
                       : MvcHtmlString.Create("");
        }        
    
        public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, RouteValueDictionary routeValueDictionary )
        {
            return htmlHelper.HasActionPermission(actionName, "")
                       ? htmlHelper.ActionLink(linkName, actionName, routeValueDictionary)
                       : MvcHtmlString.Create("");
        }
    
        public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, object routeValues, object htmlAttributes )
        {
            return htmlHelper.HasActionPermission(actionName, "")
                       ? htmlHelper.ActionLink(linkName, actionName, routeValues, htmlAttributes)
                       : MvcHtmlString.Create("");
        }
    
        public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, string controllerName)
        {
            return htmlHelper.HasActionPermission(actionName, controllerName)
                       ? htmlHelper.ActionLink(linkName, actionName, controllerName)
                       : MvcHtmlString.Create("");
        }
    
        public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            return htmlHelper.HasActionPermission(actionName, controllerName)
                       ? htmlHelper.ActionLink(linkName, actionName, controllerName, routeValues, htmlAttributes)
                       : MvcHtmlString.Create("");
        }
    }
    

    Warning: This won’t work in MVC 5 because the call to FindAction() never returns an action descriptor

    I tried to find the issue and couldn’t and ended up programming a work around. 🙁

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

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer All pooled threads are background threads, meaning they terminate automatically… May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer Since your widget and the page are hosted under different… May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer As a text node is also a node for CSS… May 15, 2026 at 4:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.