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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:39:04+00:00 2026-05-31T19:39:04+00:00

I am building a ASP.NET MVC App that will allow a user to connect

  • 0

I am building a ASP.NET MVC App that will allow a user to connect to one of several databases (with the same schema) depending upon their selection. The user will select a database prior to login and then be authenticated against that database.

I have added added several role providers to web.config, each with a different connection string corresponding to each of the available databases.

I understand that I can access the role providers with the Roles.providers collection but I don’t understand how I can choose which role provider is being user by the [Authorize] attribute in my controllers

My Controllers look something like this:

namespace MyApp.Controllers
{
    [Authorize(Roles = "admin")]
    public class AdminController : Controller
    {

I assume that this will check the users role against the default role provider.
How can I select at run time which role provider will be used for the [Authorize] attribute?

  • 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-31T19:39:05+00:00Added an answer on May 31, 2026 at 7:39 pm

    As far as I know, there’s no way to do what you’re suggesting. Attributes are static metadata that can’t be changed at run-time. However, even though it’s less convenient, you can accomplish what you want by redirecting if the user doesn’t have the appropriate role:

    [Authorize]
    public class AdminController : Controller {
    
      public ActionResult ActionRequiringRoleFoo() {
        if( !User.IsInRole( "foo" ) ) return RedirectToAction( "InsufficientPrivileges" );
        return View();
      }
    

    If you want to have this behavior for all actions in your controller, you can override the OnActionExecuting method:

    protected override void OnActionExecuting( ActionExecutingContext filterContext ) {
      base.OnActionExecuting( filterContext );
      if( filterContext.ActionDescriptor.ActionName != "InsufficientPrivileges" && 
        !User.IsInRole( "anon" ) ) filterContext.Result = new RedirectResult( "InsufficientPrivileges" );
    }
    

    I hope that helps.

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

Sidebar

Related Questions

I am building an ASP.Net MVC app that will run on a shared hosting
I am building a ASP.NET Mvc app. I have a Data model say User
I have an ASP.NET MVC app that I have been building, and I am
I am building an ASP.NET MVC 3 app that has both a www front-end
I'm building an ASP.NET MVC 3 app and I've got a model that looks
I'm building my first ASP.NET MVC 3 app and using jqGrid. One of my
I am semi-new to ASP.NET MVC. I am building an app that is used
I'm building an ASP.NET MVC app and I'm using a repository to store and
I am building my first ASP.Net MVC based app and have a problem accessing
My team is considering building our next web app using the ASP.NET MVC framework.

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.