Using Virtual User

Using Virtual User

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Security.Accounts.Role
  • Sitecore.Security.Accounts.User
  • Sitecore.Security.Accounts.UserDelegation
  • Sitecore.Security.Accounts.UserSwitcher
  • Sitecore.Security.Authentication.AuthenticationManager
  • Sitecore.Security.Domains.Domain
  • Sitecore.Security.UserProfile
  • System.Security.Principal.IIdentity

C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string userName = @"Demo\vuser";
Sitecore.Security.Accounts.User user = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(userName,true); 
if (item == null) return;

string roleName = @"Demo\DemoRole";
Sitecore.Security.Accounts.Role demoRole = Sitecore.Security.Accounts.Role.FromName(roleName);
if (demoRole == null) return;

if (Sitecore.Security.Accounts.Role.Exists(roleName) && !demoRole.IsMember(user, true, false))
{
    user.Roles.Add(Sitecore.Security.Accounts.Role.FromName(roleName)); 
}

Sitecore.Security.UserProfile profile = user.Profile; 
profile.FullName = "Virtual User"; 
profile.IsAdministrator = true;
profile.Save(); 
Sitecore.Security.Authentication.AuthenticationManager.Login(user.Name);

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag(); //Div

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + user.Name + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Display Name", "<b>" + user.DisplayName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Local Name", "<b>" + user.LocalName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Account Type", "<b>" + user.AccountType.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Description", "<b>" + user.Description + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Domain", "<b>" + user.GetDomainName() + "</b><br/>");

Sitecore.Security.Domains.Domain domain = user.Domain;
if (domain != null)
{
    output.Write("{0}&#58;&#160;{1}", "Account Prefix", "<b>" + domain.AccountPrefix + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Domain Name", "<b>" + domain.Name + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Everyone Role Name", "<b>" + domain.EveryoneRoleName + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Anonymous User Name", "<b>" + domain.AnonymousUserName + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Anonymous User Email Pattern", "<b>" + domain.AnonymousUserEmailPattern + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Member Pattern", "<b>" + domain.MemberPattern + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Ensure Anonymous User", "<b>" + domain.EnsureAnonymousUser.ToString() + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Is Default", "<b>" + domain.IsDefault.ToString() + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Locally Managed", "<b>" + domain.LocallyManaged.ToString() + "</b><br/>");
}

Sitecore.Security.Accounts.UserDelegation delegation = user.Delegation;
if (delegation != null)
    output.Write("{0}&#63;&#160;{1}", "Can Manage Domain", "<b>" + delegation.CanManage(domain).ToString() + "</b><br/>");

System.Security.Principal.IIdentity identity = user.Identity;
if (identity != null)
{
    output.Write("{0}&#58;&#160;{1}", "Identity Name", "<b>" + identity.Name + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Authentication Type", "<b>" + identity.AuthenticationType + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Identity Is Authenticated", "<b>" + identity.IsAuthenticated.ToString() + "</b><br/>");
}

output.Write("{0}&#63;&#160;{1}", "Is Administrator", "<b>" + user.IsAdministrator.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is Authenticated", "<b>" + user.IsAuthenticated.ToString() + "</b><br/>");

Sitecore.SecurityModel.UserRuntimeSettings settings = user.RuntimeSettings;
if (settings != null)
    output.Write("{0}&#63;&#160;{1}", "Is Virtual", "<b>" + settings.IsVirtual.ToString() + "</b>");

using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
    Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
    Sitecore.Data.Items.Item target = db.GetItem("/sitecore/content/home/developers");

    if (target != null)
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
        output.Write("{0}", "Developers Page access by virtual user");
        output.RenderEndTag(); //P

        output.Write("{0}&#58;&#160;{1}", "Page Title", "<b>" + target["title"] + "</b>");
    }
}
output.RenderEndTag(); //Div


Virtual User C# Web Control Output


Name: Demo\vuser
Display Name: Demo\vuser
Local Name: vuser
Account Type: User
Description: User
Domain: demo
Account Prefix: demo\
Domain Name: demo
Everyone Role Name: demo\Everyone
Anonymous User Name: demo\Anonymous
Anonymous User Email Pattern: 
Member Pattern? demo\*
Ensure Anonymous User? True
Is Default? False
Locally Managed? False
Can Manage Domain? True
Identity Name: Demo\vuser
Authentication Type: 
Identity Is Authenticated? True
Is Administrator? True
Is Authenticated? True
Is Virtual? True

Developers Page access by virtual user

Page Title: Sitecore Developers

Notes

  • Specify that the virtual user is authenticated by using true for the second argument

Sources