Archive for the ‘Sitecore 5’ Category

Virtualuser in Sitecore 5.3

04/05/2009 Leave a comment

If you want to create a user that doesn’t persist in database a virtualuser is the way to go.
It is very simple to setup the user and login him/her into sitecore. The code is ready to integrate into your own little virtualuser class. It is possible to setup most of the ordinary sitecore user item properties, but it is not required, in the following I made possible to set if the user should have Administrator rights or not.
We off course needs to include the sitecorekernel.dll V5.3

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.SecurityModel;
public class MyVirtualUser{
//Setup correct domain for user
private readonly Domain _domain = new Domain("sitecore", "security");
//This is a Sitecore useritem

private UserItem _userItem;

public User(string userName, bool isAdministrator)

//Make new id need for creating the virtual user
ID userId = ID.NewID;<br>

//Build the virtualuser, from the id and the given username
_userItem = _domain.BuildVirtualUser(userId, userName);
//We need to go into "editmode" or edit the flag for the setting
// isAdminsitrator
using (new EditContext(_userItem.InnerItem))
_userItem.IsAdministrator = isAdministrator;
public bool Login(){
//Try to login the user
DomainAccessResult accessResult = _domain.Login(_userItem);
return accessResult.Success;

A followup to sitecore 6 willl be out soon.

Categories: C#, Sitecore 5 Tags: ,