SaaS CMS has officially launched! Learn more now.

Martin Helgesen
Nov 30, 2010
  10423
(3 votes)

Virtual Roles and access control in EPiServer

In some scenarios in EPiServer you may need to create your own logic for accessing specific pages.
This can be done in several ways, but one of them is to implement your own Virtual Role. EPiServer default includes a set of virtual roles which are being used frequently.

Let’s say you have an open webportal in your company, no need to log in. And one department in the company, located in another part of the world, should see a set of pages (and its children), while the rest of the company should not. This department has specific IP addresses so we can recognize a request from this specific department from the IP address.

One solution would be to create a virtual role that compares the request IP to a set of pre-defined IP ranges (e.g from a config file). Let’s call this role “Employee”. you have to register it in web.config like this (added to the bottom after the default roles):

<virtualRoles replacePrincipal="true">
<providers>
<add name="Administrators" type="EPiServer.Security.WindowsAdministratorsRole, EPiServer" />
<add name="Everyone" type="EPiServer.Security.EveryoneRole, EPiServer" />
<add name="Authenticated" type="EPiServer.Security.AuthenticatedRole, EPiServer" />
<add name="Anonymous" type="EPiServer.Security.AnonymousRole, EPiServer" />
<add name="Creator" type="EPiServer.Security.CreatorRole, EPiServer" />
<add name="Employee" type="YourType, YourAssembly" />
</providers>
</virtualRoles>

Then you have to create the implementation of the class. Inherit from the EPiServer.Security.VirtualRoleProviderBaseclass. Here is a class that compares the requests IP address:

public class EmployeeRole : EPiServer.Security.VirtualRoleProviderBase
    {
        public override bool IsInVirtualRole(IPrincipal principal, object context)
        {
            var clientIpAddress = HttpContext.Current.Request.UserHostAddress;
            var validAddresses = new System.Xml.XmlDocument();
            validAddresses.Load(System.Web.HttpContext.Current.Server.MapPath("/ipaddresses.config"));
            var isequal = validAddresses.SelectNodes("//IPSet/ip").Cast().Any(node =&gt; IsInRange(clientIpAddress, node));
            return isequal;
        }   private static bool IsInRange(string clientIpAddress, XmlNode node)
        {
            byte[] clientIP = IPAddress.Parse(clientIpAddress).GetAddressBytes();
            byte[] mask = IPAddress.Parse(node.Attributes["mask"].Value).GetAddressBytes();
            byte[] ip = IPAddress.Parse(node.Attributes["address"].Value).GetAddressBytes();
            bool isequal = true;
            for (int i = 0; i &lt; ip.Length; i++)
                if ((clientIP[i] &amp; mask[i]) != ip[i])
                {
                    isequal = false;
                    break;
                }
            return isequal;
        }
    }

There you are finished creating your role!
But how do you assign this role read-rights to a specific page and its subpages? Do it in edit mode. But first, you have to create a new group with the same name as the role you just created. EPi will then automatically associate this group with your created Virtual Role.
After this, you can just add access rights for the group “Employee” to the pages in EPi you want.

Nov 30, 2010

Comments

Arild Henrichsen
Arild Henrichsen Jan 3, 2011 09:33 AM

Just used the same technique in a Norwegian government agency project I was working on, quick and easy to implement.
Allan Thraen previously blogged about this:
http://labs.episerver.com/en/Blogs/Allan/Dates/2009/9/I-am-virtually-in-the-role-dude/
http://labs.episerver.com/en/Blogs/Allan/Dates/2010/1/Virtual-Roles-and-Visitor-Segmentation/
(source code for sample Virtual Roles can be found at http://virtualroles.codeplex.com/)

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024