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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:53:31+00:00 2026-05-27T05:53:31+00:00

I’m working on some Role-based security for our app and I essentially want to

  • 0

I’m working on some Role-based security for our app and I essentially want to do customized verison MVC’s AuthorizeAttribute – but only at the business logic layer, where we don’t link to MVC.

I’ve looked at PrincipalPermissionAttribute but it seems it doesn’t have a way to customize it as it’s sealed. I just want to create a custom version where I can check for membership in any of a list of roles without using multiple attributes, and also define where to look for the role membership.

Is there anything like this in .Net that I’m missing? Or does anybody have some insight on how to do this without reimplementing ASP.Net’s AuthorizeAttribute/RoleProvider/etc?

EDIT

I currently have a imperative version running, but I’d rather have a declarative-attribute version, as it’s easier to see it above the method/class.

Right now I have the following in an abstract base class for my business layer:

protected void EnsureEditorLevelAccess()
{
    var allowedRoles = new[]
                            {
                                Roles.Administrator,
                                Roles.Editor,
                            };

    var roles = GetAccountRoles(GetCurrentUsername());

    if (roles.Any(role => allowedRoles.Contains(role)))
    {
        return;
    }

    throw new SecurityException("You do not have sufficient privileges for this operation.");
}

I like being able to use Roles.Administrator etc because the role names are hideous (Active Directory group based…), so I was thinking of wrapping those details up in the constructor of a custom attribute that I can just plop on top of classes/methods.

GetAccountRoles is just a facade over an injectable role-provider property, which I can set to use either AD or a testing version that uses the database.

I could subclass Attribute, but not sure how it would kick off the security check.

  • 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-27T05:53:32+00:00Added an answer on May 27, 2026 at 5:53 am

    You can create a new attribute that uses the existing PrincipalPermission if that would be sufficient for your needs. If your existing imperative implementation uses PrincipalPermission, then this should be the case. However, if your imperative version does something else, you may need to consider implementing both a custom permission and a corresponding attribute. If you’re not sure whether this is necessary, perhaps you could share some details regarding your current imperative approach…


    After question update…

    It’s actually possible to use “any” logic with PrincipalPermission, although it requires unioning of multiple instances, which is not particularly practical to work with in an attribute. This makes it much more reasonable to create a custom attribute, which might look something like the following:

    [Serializable]
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    public sealed class AnyRolePermissionAttribute : CodeAccessSecurityAttribute
    {
        public AnyRolePermissionAttribute(SecurityAction action)
            : base(action)
        {
        }
    
        public string Roles { get; set; }
    
        public override IPermission CreatePermission()
        {
            IList<string> roles = (this.Roles ?? string.Empty).Split(',', ';')
                                    .Select(s => s.Trim())
                                    .Where(s => s.Length > 0)
                                    .Distinct()
                                    .ToList();
    
            IPermission result;
            if (roles.Count == 0)
            {
                result = new PrincipalPermission(null, null, true);
            }
            else
            {
                result = new PrincipalPermission(null, roles[0]);
                for (int i = 1; i < roles.Count; i++)
                {
                    result = result.Union(new PrincipalPermission(null, roles[i]));
                }
            }
    
            return result;
        }
    }
    

    Unfortunately, you can’t use arrays in security attributes, so the role list has to be represented as a string. e.g.:

    [AnyRolePermission(SecurityAction.Demand, Roles = "Foo, Bar")]
    

    You could use it with your constants via design-time concatenation. e.g.:

    [AnyRolePermission(SecurityAction.Demand, Roles = Roles.Administrator + ", " + Roles.Editor)]
    

    As for your custom role provider, the appropriate place to use it is in the thread principal, not the permission or attribute. For example, if you’re currently using a GenericPrincipal, you could replace it with a custom principal that uses your custom role provider to retrieve the target identity’s roles.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.