Anders Hattestad
Jan 19, 2011
visibility 8865
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
Finding Thomas Part 3 - The Moment of Recognition

Remember Thomas? In digital landscape, Thomas is the returning visitor who reads everything, opens every email, converts on nothing. In standard...

Ritu Madan | Jun 26, 2026

Add more scheduled job settings from the Optimizely CMS 12 admin UI -- with OptiScheduledJob.ExtraParameters

  Optimizely (EPiServer) CMS 12 ships a great scheduled-jobs framework, but it has one frustrating gap: a job has nowhere to store its own...

Binh Nguyen Thi | Jun 25, 2026

Automated Search & Navigation to Graph Migration with Claude Code

A Claude Code plugin that scans your S&N codebase, applies Graph SDK transformations, and validates the result. Install once, run one command. CMS ...

Connor Fortin | Jun 24, 2026

Migrating from Find to Graph: Lessons Learned from a Real CMS 13 Project

While migrating a search solution from Optimizely Search & Navigation (Find) to Optimizely Graph in CMS 13, I encountered several issues that were...

Binh Nguyen Thi | Jun 24, 2026