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
- public override void AddUsersToRoles(string[] usernames, string[] roleNames)
- {
- var providersXRoles = GetProvidersXRoles(roleNames);
- foreach (RoleProvider roleProvider in providersXRoles.Keys)
- roleProvider.AddUsersToRoles(usernames, providersXRoles[roleProvider].ToArray());
- }
- public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
- {
- var providersXRoles = GetProvidersXRoles(roleNames);
- foreach (RoleProvider roleProvider in providersXRoles.Keys)
- roleProvider.RemoveUsersFromRoles(userNames, providersXRoles[roleProvider].ToArray());
- }
- private Dictionary<RoleProvider, List<string>> GetProvidersXRoles(string[] roleNames)
- {
- var providersXRoles = new Dictionary<RoleProvider, List<string>>();
- foreach (var roleName in roleNames)
- {
- foreach (var provider in DistinctProviders)
- {
- if (provider.RoleExists(roleName))
- {
- if (!providersXRoles.ContainsKey(provider))
- providersXRoles.Add(provider, new List<string>());
- providersXRoles[provider].Add(roleName);
- break;
- }
- }
- }
- return providersXRoles;
- }
and the find user roles etc
- public override string[] FindUsersInRole(string roleName, string usernameToMatch)
- {
- var list = new List<string>();
- foreach (RoleProvider provider in this.DistinctProviders)
- list.AddRange(provider.FindUsersInRole(roleName, usernameToMatch));
- return list.ToArray();
- }
- public override string[] GetRolesForUser(string username)
- {
- var list = new List<string>();
- foreach (var provider in DistinctProviders)
- list.AddRange(provider.GetRolesForUser(username));
- return list.ToArray();
- }
- public override string[] GetUsersInRole(string roleName)
- {
- var list = new List<string>();
- foreach (RoleProvider provider in this.DistinctProviders)
- if (provider.RoleExists(roleName))
- list.AddRange(provider.GetUsersInRole(roleName));
- return list.ToArray();
- }
- public override bool IsUserInRole(string username, string roleName)
- {
- foreach (var provider in DistinctProviders)
- if (provider.RoleExists(roleName))
- return provider.IsUserInRole(username, roleName);
- return false;
- }
This change will not use the providerMap1 attributes on the role provider section
- <roleManager enabled="true" defaultProvider="MultiplexingRoleProvider" cacheRolesInCookie="true">
- <providers>
- <clear />
- <!-- Comment the following lines when running on oracle. -->
- <add name="MultiplexingRoleProvider" type="Itera.Security.MultiplexingRoleProvider, Itera.Security"
- provider1="SqlServerRoleProvider"
- provider2="WindowsRoleProvider"
- provider3="EPiServerRoleProvider" />
- <add name="WindowsRoleProvider" applicationName="EPiServerSample" type="EPiServer.Security.WindowsRoleProvider, EPiServer" />
- <add name="SqlServerRoleProvider" connectionStringName="EPiServerDB" applicationName="EPiServerSample" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
- <add name="EPiServerRoleProvider" applicationName="EPiServerRoleProvider" type="Itera.Security.EPiServerRoleProvider, Itera.Security" />
- </providers>
- </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.
Comments