Get Active User

Get Active User

Domain Account Names

  • All strings containing account names must include the domain name in the format domainName\accountName

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.Security.Accounts.Role
  • Sitecore.Security.Accounts.User
  • Sitecore.Security.Accounts.UserRoles
  • Sitecore.Security.Authentication.AuthenticationManager

Active User C# Web Control Code

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

Sitecore.Security.Accounts.User activeUser = Sitecore.Security.Authentication.AuthenticationManager.GetActiveUser();
if (activeUser == null) return;

Sitecore.Security.Accounts.UserRoles userRoles = activeUser.Roles;
if (userRoles == null) return;

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();

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}", "User Name", "<b>" + activeUser.Profile.UserName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + activeUser.Name.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Full Name", "<b>" + activeUser.Profile.FullName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Email", "<b>" + activeUser.Profile.Email + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Domain", "<b>" + activeUser.Domain.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is Administrator", "<b>" + activeUser.IsAdministrator.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is Authenticated", "<b>" + activeUser.IsAuthenticated.ToString() + "</b><br/>");

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("Roles That Include Current User");
output.RenderEndTag(); //P

foreach (Sitecore.Security.Accounts.Role role in userRoles)
{
    output.Write("{0}", "<b>" + role.Name + "</b><br/>");
}
output.RenderEndTag(); //Div


Active User C# Web Control Output


User Name: extranet\Anonymous
Name: extranet\Anonymous
Full Name: 
Email: 
Domain: extranet
Is Administrator? False
Is Authenticated? False

Roles That Include Current User

Sources