Understanding Sitecore Role And Virtual User Creation With Use Case
Hello everyone, I am back with another blog. In this blog we will try to understand, how we can create a new Sitecore Domain, how we can create a new Sitecore Role and how we can use the role to restrict the user in backend system from accessing the items in Sitecore. Will try to keep this blog as short as possible.
So let's begin. We will try to understand all the above concepts using a use case. This came as a requirement for one of our project.
Recently, we got an requirement to restrict some of the logged in user in our website from accessing some of the pages in Sitecore. To overcome this requirement, we have done it by implementing Sitecore Roles and Concept of virtual user in our code behind. Below is the steps which I followed.
Step 1: Create a new domain in Sitecore. There are few inbuilt domain which Sitecore automatically provides like Extranet, Sitecore etc. But for our understanding we will create everything new. Go to launchpad once you have logged into Sitecore. In launchpad, we will see Access management section as shown below. Just click on Domain Manager.
Once you have clicked Domain Manager, you will get an option of new in the ribbon. Just click new button to create a new Domain. Give a domain name and check the checkbox as shown below and click ok. Our domain will be created now. For our case we given the domain name as SitecoreUnauthorizeServicePageUser.
Step 2: Create a new role. We will select the Role Manager option from Sitecore launchpad.The next step will be to click on New button on ribbon and it will open a popup. Give a suitable name to your role and just select the newly created domain from the dropdown.See below image for the reference. In our case we have given the role name as UnAuthorizedServicePageUser
Step 3: Once our role is created, now select the Security Editor from Sitecore launchpad. Now select the required Sitecore item from tree in left side pane,which we have to restrict from accessing. Once you have selected, then click on assign button in the ribbon. Please see the below image for your referenece.
Step 4: Once you click on Assign button, you will get an popup to select the role and give the access for the role for that item. As shown in the below image, remove the read access for that item. You can also restrict the user from accessing the decendants of the items too. Then click OK once done.
From Sitecore Side, we are done. Now let's see the code behind logic, that how we can assign a role to logged in User. To accomplish the same, we will build a virtual user, assign them the required role which will help to restrict the user from accessing the Sitecore pages. The code is pretty straight forward. After user logged in, you can take email and create a virtual user for him. Please refer the below code snippet for the same.
try
{
string email = "abc@test.com"
//Creating virtual user using domain + email
var virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser("SitecoreUnauthorizeServicePageUser" + "\\" + email, true);
//Adding role to that virtual user
virtualUser.Roles.Add(Sitecore.Security.Accounts.Role.FromName(SitecoreUnauthorizeServicePageUser\UnAuthorizedServicePageUser));
//Creating profile for the virtual user
virtualUser.Profile.Email = email;
virtualUser.Profile.Save();
//Authenticating the virtual user
Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(virtualUser);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("VirtualUser", ex, this);
}
Once your deploy the code, you can try to access the page for the user and it will not allow.
Thanks for reading and Happy learning.
You can check my other blogs too if interested. Blog Website
Comments
Post a Comment