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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:14:05+00:00 2026-06-09T15:14:05+00:00

I’ve got an MVC3 application with 4 levels of authentication, and 4 base controllers

  • 0

I’ve got an MVC3 application with 4 levels of authentication, and 4 base controllers that tie to each one:

  1. Unauthenticated – BaseController
  2. User – BaseAuthController : BaseController
  3. Advisor – BaseAdvisorController : BaseAuthController
  4. Admin – BaseAdminController : BaseAuthController

Right now I have a series of overrides in place for special cases… e.g. a controller that is typically only for admins can have an action method or two that advisors can use… I have the overrides defined as strings in an array.

public class BaseAuthController : BaseController
{
    /// <summary>
    /// Enter action names in here to have them ignored during login detection
    /// </summary>
    public string[] NoAuthActions = new string[] { };

    /// <summary>
    /// Actions only usable by Users+
    /// </summary>
    public string[] UserOnlyActions = new string[] { };

    /// <summary>
    /// Actions only usable by Advisors+
    /// </summary>
    public string[] AdvisorOnlyActions = new string[] { };

    /// <summary>
    /// Actions only usable by Admins+
    /// </summary>
    public string[] AdminOnlyActions = new string[] { };

    .......

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //special code here to determine what to do with requested action...
        //verifies that user is logged in and meets requirements for method...
        //if not, redirects out to another page...
    }
}

At the controller level I have them defined like this…

public class GrowerController : BaseAdminController
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        UserOnlyActions = new string[] { "GrowthStageSelection" };
        AdvisorOnlyActions = new string[] { "Landing", "SeedSelection", "UpdateProjection",
                                            "NitrogenApplications", "DeleteNitrogen", "MassUpload",
                                            "VerifyHolding", "ConfirmHolding", "DeleteHoldingDir", "DeleteHoldingFile" };
        base.OnActionExecuting(filterContext);
    }

    //......

    [HttpPost]
    public ActionResult GrowthStageSelection(int growerID, int reportGrowthStageID = 0)
    {
        //code...
    }
}

This system has actually worked out pretty well for us, but the problem for me has been that it feels messy. You have to define the methods one place, and override their authentication level elsewhere if necessary. If you change the method name you have to remember to change it elsewhere.

What I’d LOVE to be able to do is decorate the methods themselves with authentication specific attributes and do away the string-based definitions (or at least make them transparent and use List<string> dynamically or something). Here’s an example of what I’m looking for…

    [HttpPost]
    [AdvisorAuthentication]
    public ActionResult GrowthStageSelection(int growerID, int reportGrowthStageID = 0)
    {
        //code...
    }

Problem is that I can’t find a good way to achieve this with attributes. I’ve tried creating subclasses of ActionFilterAttribute but they run after my BaseAuthController‘s override for OnActionExecuting. At that point it’s too late in the game to add new methods to the string lists dynamically, and moreover I can’t even seem to access the current controller instance from the attributes.

Maybe this whole idea is off base. Can anyone point me in the right direction? Thanks.

Final solution

First, I went ahead and deleted all of my special controllers except for BaseController – I had no use for them anymore. I moved the current special authentication code from BaseAuthController into BaseController. Next, I defined a series of attributes for each of my authentication states:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class BaseAuthAttribute : Attribute
{
    public AuthLevels AuthLevel { get; protected set; }

    public BaseAuthAttribute(AuthLevels level)
    {
        this.AuthLevel = level;
    }

    public override string ToString()
    {
        return string.Format("Auth Required: {0}", this.AuthLevel.ToString());
    }
}

public class UnauthenticatedAccess : BaseAuthAttribute
{
    public UnauthenticatedAccess()
        : base(AuthLevels.Unauthenticated)
    {
    }
}

public class UserAccess : BaseAuthAttribute
{
    public UserAccess()
        : base(AuthLevels.User)
    {
    }
}

public class AdvisorAccess : BaseAuthAttribute
{
    public AdvisorAccess()
        : base(AuthLevels.Advisor)
    {
    }
}

public class AdminAccess : BaseAuthAttribute
{
    public AdminAccess()
        : base(AuthLevels.Admin)
    {
    }
}

Then in my BaseController I modified the OnActionExecuting to check the current auth level of the logged in user (if any) against the attribute. This is much cleaner than it was before! (Note: SessionUser and AuthLevels are custom objects for our project – you won’t have those)

public partial class BaseController : Controller
{
    /// <summary>
    /// Override security at higher levels
    /// </summary>
    protected bool SecurityOverride = false;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        BaseAuthAttribute authAttribute = filterContext.ActionDescriptor.GetCustomAttributes(false).OfType<BaseAuthAttribute>().FirstOrDefault();
        if (authAttribute == null) //Try to get attribute from controller
            authAttribute = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(false).OfType<BaseAuthAttribute>().FirstOrDefault();
        if (authAttribute == null) //Fallback to default
            authAttribute = new UnauthenticatedAccess(); //By default, no auth is required for base controller

        if (!SessionUser.LoggedIn
            && authAttribute.AuthLevel == AuthLevels.Unauthenticated)
        {
            SecurityOverride = true;
        }
        else if (SessionUser.LoggedIn
            && SessionUser.LoggedInUser.AuthLevel >= (int)authAttribute.AuthLevel)
        {
            SecurityOverride = true;
        }

        if (!SessionUser.LoggedIn && !SecurityOverride)
        {
            //Send to auth page here...
            return;
        }
        else if (!SecurityOverride)
        {
            //Send somewhere else - the user does not have access to this
            return;
        }

        base.OnActionExecuting(filterContext);
    }

    // ... other code ...
}

That’s it! Now just put it to use like so…

[AdminAccess]
public class GrowerController : BaseController
{
    public ActionResult Index()
    {
        //This method will require admin access (as defined for controller)
        return View();
    }

    [AdvisorAccess]
    public ActionResult Landing()
    {
        //This method is overridden for advisor access or greater
        return View();
    }
}
  • 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-06-09T15:14:06+00:00Added an answer on June 9, 2026 at 3:14 pm

    If I understood your question properly, you can implement your own custom attributes (not authorisation attributes) and in the overriden OnActionExecuting of the base controller, you can retrieve the custom attributes of the executing method and based on wich ones are defined you can take appropriate actions. So if a method has the [AdvisorAuthentication] you know that you need to check for those credentials before proceeding.

    EDIT:
    I don’t have an example to point you to as this is something I have implemented in one of my projects. I have no access to that code now but here is an outline:

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            IEnumerable<MyCustomAttribute> attributes = filterContext.ActionDescriptor.GetCustomAttributes(false).OfType<MyCustomAttribute>();
            foreach (MyCustomAttributeobj in attributes)
            {
                switch(MyCustomAttribute.AttribType){
                    case MyCustomeAttribute.AdvisorAuthentication:
    
                        break;
                    case MyCustomeAttribute.AdminAuthentication:
    
                        break;
                }
            }
        }
    

    You can implement just one custom attribute MyCustomAttribute and have it accept a parameter to indicate which authorization type you want. Like that the use of the attribute becomes [MyCustomAttribute(“MyCustomeAttribute.AdminAuthentication”)]

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but

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.