C# Architecture – Role Provider for Multiple Applications

Architecturecroles

I'm making a custom RoleProvider that I would like to use across multiple applications in the same application pool. For the administration of roles (create new role, add users to role, etc..) I would like to create a master application that I could login to and set the roles for each additional application. So for example, I might have AppA and AppB in my organization, and I need to make an application called AppRoleManager that can set roles for AppA and AppB. I am having troubles implementing my custom RoleProvider because it uses an initialize method that gets the application name from the config file, but I need the application name to be a variable (i.e. "AppA" or "AppB") and passed as a parameter. I thought about just implementing the required methods, and then also having additional methods that pass application name as a parameter, but that seems clunky. i.e.

public override CreateRole(string roleName)
{
  //uses the ApplicationName property of this, which is set in web.config
  //creates role in db
}
public CreateRole(string ApplicationName, string roleName)
{
  //creates role in db with specified params.
}

Also, I would prefer if people were prevented from calling CreateRole(string roleName) because the current instance of the class might have a different applicationName value than intended (what should i do here? throw NotImplementedException?).

I tried just writing the class without inheriting RoleProvider. But it is required by the framework.

Any general ideas on how to structure this project?

I was thinking make a wrapper class that uses the role provider, and explicitly sets the application name before (and after) and calls to the provider something like this:

static class RoleProviderWrapper
{
  public static CreateRole(string pApplicationName, string pRoleName)
  {
    Roles.Provider.ApplicationName = pApplicationName;
    Roles.Provider.CreateRole(pRoleName);
    Roles.Provider.ApplicationName = "Generic";
  }
}

is this my best-bet?

Best Answer

If you ran the aspnet_regsql.exe that should give you a great start and you wont need to re-invent the wheel (at least to get started). It implements the default Membership provider and gives you a lot of "off the shelf" functionality, which is what you're describing.. user login, remember password, attribute driven authentication (for MVC), directory level permissioning, and configuration based login redirects, session timeouts etc..

The aspnet_regsql tool gives you an empty database schema and makes it super easy to tie the default membership behavior into your own app including; Role based folder access, multiple applications with single sign on support, and some other membership / profile type tables (i generally dont use them much)).

You can also manage users / roles from Visual Studio using the website configuration tools from the web based management website.. and this is all built in and available without any additional coding / plug-ins.. i highly recommend it for new apps since it gives you so much in so little time and lets you focus on the fun part (the app)..

Also, you can carve down what's generated from there, or use it as an example. I usually just tie in their User_ID into the user of my schema and put a trigger or "on user created" event to sync the GUID's. That way i can get a user login, retreive password, and remember me up and running in about 10 minutes (it also handles situations where cookies aren't enabled).. and if there is ever a need to implement a custom provider, you can circle back, but at least the underlying functionality doesnt stop the app development from moving forward.

some info about aspnet_regsql.exe from MS:

http://msdn.microsoft.com/en-us/library/ms229862(v=vs.100).aspx

Here's a much better walkthrough:

http://weblogs.asp.net/sreejukg/archive/2011/06/16/usage-of-aspnet-regsql-exe-in-asp-net-4.aspx