In my recent project we got a requirement where need to manage client current site user’s into Sitecore. First we thought to get User DB from client side and configure that with our current solution and use them in current implementation. But as far as client is having there own team to manage there UMS (user management system) which is build in java also they are regularly updating user details in there system. And it was a huge data of user ‘s which was very difficult to manage in Sitecore.
At the final staged we agreed to get the data or user details with the help of web services.
For example if we need to validate user for login than we need to provide the username and password of user and web service was resposible to validate the user as well as provide the required details of user for example – Name, email id, phone etc.
Now here we started thinking to manage that web service user as Sitecore extranet user so we don’t need to worry about the security permission with Sitecore. Because many of the items in content tree was covered under Role based security.
First we started thinking to create a user in Sitecore at the time of login and then consider the user as extranet users and at the time of log out remove that user from the Sitecore. But after some search on Google I found Virtual user concept and it was really nice to manage the same thing which we were looking for because we were also looking for the virtual set up of user not completely into Sitecore. But after creating virtual user it work as a Sitecore extranet user.
Here we move with our study on virtual user –
At the time of login user is providing username and password and click on sign in button first we are calling the web service that is basically validating the user from client UMS and than sending the user details in response.
Once we are getting the valid user response then we created virtual user and assign the values of user properties, below is the sample API to create virtual user in Sitecore –
var virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(userName, true);
here username is domain/username.
We also need to maintains multiple custom profile properties so we have created custom user profile item as given in this post.
Below is the sample API for assigning the custom values to custom user profile.
virtualUser.Profile.FullName = webserviceResponse.response.firstName ?? String.Empty + webserviceResponse.response.lastName ?? String.Empty; virtualUser.Profile.Email = webserviceResponse.response.email ?? String.Empty; virtualUser.Roles.Add(Sitecore.Security.Accounts.Role.FromName("domain\\RoleName")); virtualUser.Profile.SetCustomProperty(Constants.UserCustomProfileCons.FirstName, webserviceResponse.response.firstName ?? String.Empty); virtualUser.Profile.SetCustomProperty(Constants.UserCustomProfileCons.LastName, webserviceResponse.response.lastName ?? String.Empty); virtualUser.Profile.SetCustomProperty(Constants.UserCustomProfileCons.profileUID, webserviceResponse.response.profileUID ?? String.Empty); virtualUser.Profile.Save();
After storing all the custom profile values with virtual user we logged in virtual user by using Sitecore Authentication code as given below –
Now this work as same as Sitecore extranet user login. If we want to check if user is authenticated or not we can use the Sitecore API for same as below –
Sitecore.Context.User.IsAuthenticated; This API with return true if you are logged in with Virtual user
Below are some examples to get user details on other pages after login with virtual user –
//To Get the username of current logged in user Sitecore.Context.User.Name; // TO get the value of any of the custom property Sitecore.Context.User.Profile.GetCustomProperty("CustomPropertyName");
We have tested it with multiple login at the same time and it is working perfect for us. Please write your feedback in comment section if you find any issue with this approach or if we can make it more better.