Check For Login

Check For Login

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Security.Accounts.User

Check For Login C# Web Control Code

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

Sitecore.Security.Accounts.User user = Sitecore.Context.User;
if (user == 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(); //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}", "User Name", "<b>" + user.Name + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is Authenticated", "<b>" + user.IsAuthenticated.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is Logged In", "<b>" + Sitecore.Context.IsLoggedIn.ToString() + "</b>");

output.RenderEndTag(); //Div


Check For Login XSLT Rendering Code

<xsl:value-of select="sc:IsLoggedIn()"/></xsl:value-of>


Check For Login C# Web Control Output

User Name: extranet\Anonymous
Is Authenticated? False
Is Logged In? False

Notes

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

Sources