Anders Hattestad
Jan 19, 2011
visibility 9059
star star star star star
(3 votes)

Multiplexing role provider; more that one role provider pr user

There are times when you need to have one user provider and two role providers. The multiplexing role provider that are shipped with EPiServer only allows one role provider pr user.

Basically what I have done is to copy the hole code from reflector, and changes methods. This is done because of I wanted to be able to use the same role provider for different user providers. See blog post here. Got a ranking on 1 on that, but have to disagree that it was only worth one star :)

Then I have changes add and remove roles

Code Snippet
  1. public override void AddUsersToRoles(string[] usernames, string[] roleNames)
  2. {
  3.     var providersXRoles = GetProvidersXRoles(roleNames);
  4.     foreach (RoleProvider roleProvider in providersXRoles.Keys)
  5.         roleProvider.AddUsersToRoles(usernames, providersXRoles[roleProvider].ToArray());
  6. }
  7. public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
  8. {
  9.     var providersXRoles = GetProvidersXRoles(roleNames);
  10.     foreach (RoleProvider roleProvider in providersXRoles.Keys)
  11.         roleProvider.RemoveUsersFromRoles(userNames, providersXRoles[roleProvider].ToArray());
  12. }
  13. private Dictionary<RoleProvider, List<string>> GetProvidersXRoles(string[] roleNames)
  14. {
  15.     var providersXRoles = new Dictionary<RoleProvider, List<string>>();
  16.     foreach (var roleName in roleNames)
  17.     {
  18.         foreach (var provider in DistinctProviders)
  19.         {
  20.             if (provider.RoleExists(roleName))
  21.             {
  22.                 if (!providersXRoles.ContainsKey(provider))
  23.                     providersXRoles.Add(provider, new List<string>());
  24.                 providersXRoles[provider].Add(roleName);
  25.                 break;
  26.             }
  27.         }
  28.     }
  29.     return providersXRoles;
  30. }

and the find user roles etc

Code Snippet
  1. public override string[] FindUsersInRole(string roleName, string usernameToMatch)
  2. {
  3.     var list = new List<string>();
  4.     foreach (RoleProvider provider in this.DistinctProviders)
  5.         list.AddRange(provider.FindUsersInRole(roleName, usernameToMatch));
  6.     return list.ToArray();
  7. }
  8.  
  9. public override string[] GetRolesForUser(string username)
  10. {
  11.     var list = new List<string>();
  12.     foreach (var provider in DistinctProviders)
  13.         list.AddRange(provider.GetRolesForUser(username));
  14.     return list.ToArray();
  15. }
  16.  
  17. public override string[] GetUsersInRole(string roleName)
  18. {
  19.     var list = new List<string>();
  20.     foreach (RoleProvider provider in this.DistinctProviders)
  21.         if (provider.RoleExists(roleName))
  22.             list.AddRange(provider.GetUsersInRole(roleName));
  23.     return list.ToArray();
  24. }
  25. public override bool IsUserInRole(string username, string roleName)
  26. {
  27.     foreach (var provider in DistinctProviders)
  28.         if (provider.RoleExists(roleName))
  29.             return provider.IsUserInRole(username, roleName);
  30.     return false;
  31. }
to loop over all role providers when you add/read or change membership. If you try to add a windows security role it will throw an error, but in a friendly manner,

This change will not use the providerMap1 attributes on the role provider section

Code Snippet
  1. <roleManager enabled="true" defaultProvider="MultiplexingRoleProvider" cacheRolesInCookie="true">
  2.     <providers>
  3.         <clear />
  4.         <!-- Comment the following lines when running on oracle. -->
  5.     <add name="MultiplexingRoleProvider" type="Itera.Security.MultiplexingRoleProvider, Itera.Security"
  6.          provider1="SqlServerRoleProvider"
  7.          provider2="WindowsRoleProvider"
  8.          provider3="EPiServerRoleProvider" />
  9.     <add name="WindowsRoleProvider" applicationName="EPiServerSample" type="EPiServer.Security.WindowsRoleProvider, EPiServer" />
  10.     <add name="SqlServerRoleProvider" connectionStringName="EPiServerDB" applicationName="EPiServerSample" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  11.     <add name="EPiServerRoleProvider" applicationName="EPiServerRoleProvider" type="Itera.Security.EPiServerRoleProvider, Itera.Security" />
  12.     </providers>
  13. </roleManager>

So this is how a typical section could look like.

The SqlServerRoleProvider is the default one, and new roles created in EPiServer will be added to that one. EPiServerRoleProvider is pages in episerver that are roles. Will blog about that one later.

See full code here

Jan 19, 2011

Comments

error Please login to comment.
Latest blogs
Setting Up Cloudflare Orange-to-Orange for Optimizely DXP Customers

Large Optimizely DXP customers can often run into complex firewall, caching, and bot management requirements that don't quite fit into what is...

Jacob Pretorius | Dec 4, 2026

How to Fetch CMP DAM Listing Cards in Optimizely CMS 12 Using GraphQL

Introduction In an existing Optimizely CMS 12 application, listing cards are typically populated using CMS content retrieved through the existing...

Deepmala S | Dec 3, 2026

Which environment am I in? Adding an environment badge to Optimizely CMS 13

Sometimes it’s hard to keep track of which environment you’re working in. Development, Integration, Preproduction and Production all look pretty mu...

Pär Wissmark | Dec 2, 2026 |

Hiring Your First AI Teammate: What Optimizely's Virtual Teammates Mean for the Marketing Org Chart

The coworker who never needs the brief twice How many times this week did you paste the same context into an AI tool? Brand voice, the target...

Leonardo Bravo | Dec 2, 2026