Using Macro in Page Template Layout to Check If User is Authenticated

using-macro-in-page-template-layout-to-check-if-user-is-authenticated

This code can be used when you need to hide a section or portion of the page for user that are not logged in.

For HTML Layout example:

<div style="{% CurrentUser.IsAuthenticated ? "display:none" : "display:block" #%}">
<p>This section will be hidden from unauthenticated users. Only authenticated users will see this message.</p>
</div>

For ASCX Layout example:

<div style="<%= CMS.Membership.MembershipContext.AuthenticatedUser != null && !CMS.Membership.MembershipContext.AuthenticatedUser.IsPublic() ? "display:none" : "display:inline-block" %>">
<p>This section will be hidden from unauthenticated users. Only authenticated users will see this message.</p>
</div>

If you decide to check if the user is authenticated and then display his/her name use this code:

<% if (CMS.Membership.AuthenticationHelper.IsAuthenticated()) { %><%= CMS.Membership.MembershipContext.AuthenticatedUser.FullName %> Welcome Back!<% } %>

Also, keep in mind that Kentico allows conditional layouts using ASCX.
You can insert conditional layout elements which allows you to create flexible layouts that display content based on certain authentication criteria or tiers. The page layout renders the content between the CMSConditionalLayout tags only if the authentication conditions are true.

<div class="section">
<cms:CMSConditionalLayout runat="server" id="tier1" GroupName="Roles" VisibleForRoles="Tier1">
    <cms:CMSWebPartZone runat="server" ZoneID="Tier1" />
</cms:CMSConditionalLayout>
<cms:CMSConditionalLayout runat="server" id="tier2" GroupName="Roles" VisibleForRoles="Tier2">
    <cms:CMSWebPartZone runat="server" ZoneID="Tier2" />
</cms:CMSConditionalLayout>
<cms:CMSConditionalLayout runat="server" id="defaultLayout" GroupName="Roles" >
    <cms:CMSWebPartZone runat="server" ZoneID="Default" />
</cms:CMSConditionalLayout>
</div>

See more information creating conditional layouts.