November Happy Hour will be moved to Thursday December 5th.

Anders Hattestad
Jan 19, 2011
  8377
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog