Anders Hattestad
Jan 19, 2011
  7840
(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

Please login to comment.
Latest blogs
Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog