Home > C#, Sitecore 5 > Virtualuser in Sitecore 5.3

Virtualuser in Sitecore 5.3

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.

Advertisements
Categories: C#, Sitecore 5 Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: