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?
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:
If you want to have this behavior for all actions in your controller, you can override the
OnActionExecutingmethod:I hope that helps.