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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:13:14+00:00 2026-06-07T20:13:14+00:00

I am creating an MVC application with forms auth. I am authenticating against active

  • 0

I am creating an MVC application with forms auth. I am authenticating against active directory and so have created a custom RoleProvider. My application is only concerned with a small set of roles which up until now I have been defining in the appSettings section of my web.config:

<appSettings>
  <add key="DirectorRole" value="Domain\Directors" />
  <add key="ManagementRole" value="Domain\Managers" />
  ...
</appSettings>

However I have run into a couple of problems with this approach:

  1. I cannot reference these setting in my contoller data annotations: [Authorize(Roles = ConfigurationManager.AppSettings["DirectorRole"])] as it wont compile so I have to specify the name of the group again: [Authorize(Roles = "Domain\\Directors")].
  2. In my web.config, I would like to specify the groupsToUse for my role provider and just reference a pre-existing list, rather than maintain two seperate lists of the same set of roles.

It seems that there must be a better/reusable way to define the roles in the web.config, can someone point me in the right direction please?

  • 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-07T20:13:15+00:00Added an answer on June 7, 2026 at 8:13 pm

    I would prefer using a custom authorize attribute. Like this one.

    public class MyAuthorizeAttribute : AuthorizeAttribute {
    
        public MyAuthorizeAttribute(params string[] roleKeys) {
            List<string> roles = new List<string>(roleKeys.Length);
    
            //foreach(var roleKey in roleKeys) {
                //roles.Add(ConfigurationManager.AppSettings["DirectorRole"]);
            //}
            var allRoles = (NameValueCollection)ConfigurationManager.GetSection("roles");
            foreach(var roleKey in roleKeys) {
                roles.Add(allRoles[roleKey]);
            }
    
            this.Roles = string.Join(",", roles);
        }
    }
    

    In your controller, use:

    [MyAuthorize("DirectorRole")]
    

    In your web.config

      <configSections>
        <section
          name="roles"
          type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </configSections>
    
      <roles>
        <add key="DirectorRole" value="Domain\Directors" />
        <add key="ManagementRole" value="Domain\Managers" />
      </roles>
    

    I hope this will solve your first problem just fine. And twiking a little will solve the second one too.

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

Sidebar

Related Questions

I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I'm creating an intranet asp.net mvc application that everyone in the company should have
I am creating an MVC application framework, using only libararies and components that I
I have a MVC 3 Razor web application that needs to allow creating and
I have an MVC application that I am creating that I have to integrate
I'm creating a custom model binder in an Mvc application and I want to
I am creating Sample Spring MVC application. In my Controller class I have define
I'm currently creating an MVC application that will likely to expand to include a
I'm creating an MVC application that's following a RESTful URL approach. I am experiencing
I am working on creating an MVC application for an existing Bug tracker of

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.