Sunday, October 12, 2008

Hide the “Sign in” link in the client web browser for anonymous users


Do you want to hide the sign-in link on your SharePoint sites for your internet users that access it in anonymous authentication mode?
Well, there is one simple and very quick way to do this with 2 scripts in your master page using C# + JavaScript. The effect will be that the link will not be present for anonymous user, but authenticated users will see Welcome and Action menus.

In the head section of your master page place this code :
 <head>
     <script>
      <% 
         Response.Write("var isAuthenticated =" +System.Web.HttpContext.Current.User.Identity.IsAuthenticated.ToString().ToLower() +";");
      %>
     </script>
 </head>
Wrap your menu elements with a container and give it an Id :

         <td id="MenuElements" class="ms-MenuElements"> 
             <wssuc:Welcome id="IdWelcome" runat="server" EnableViewState="false"></wssuc:Welcome>
             <PublishingSiteAction:SiteActionMenu runat="server" />
        </td>

At the end of your master page place this javaScript code :

        <script>
        if(!isAuthenticated){
         document.getElementById('MenuElements').style.display="none";
        }
        </script>
And it's done !
Warning :
Don't forget to replace the in line code in the master page later, by an User Control or a Custom control, as the in line code will fail if the master page is customized...

7 comments:

Anonymous said...

why not use a security trimmed control instead?

Marc Charmois said...

You mean that way ?

<Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="ManageLists">
Place your control(s) here
</SharePoint:SPSecurityTrimmedControl>

Unfortunatly, there is no item in SPBasePermissions Enumeration that specifies if user is authenticated or not.

Anonymous said...

You don't need a SPBasePermissions Enumeration that specifies if user is authenticated or not. The unauthenticated user dosen't have the right to ManageLists, so it's working.

Marc Charmois said...

Ok, then Authenticated users that cannot manage lists will not see the "Sign in" link... :-)

Anonymous said...

Try this

< asp:LoginView runat="server">
< LoggedInTemplate>

authenticated users content
</LoggedInTemplate>
<AnonymousTemplate>
anonymous content
</AnonymousTemplate>
</asp:LoginView>

Anonymous said...

"asp:LoginView" is the most simplest and streight forward means of rendering Authanticarted and Anonymous content respectivly.

Salute..

Unknown said...

Just found this post as part of some research I'm doing to solve the same problem.

I'm unclear about your warning. You state "Don't forget to replace the in line code in the master page later..."

In the your example code, what code needs to be replaced, and replaced with what?

I thought I could implement your example code and like you said "And it's done." But your warning implies there is something else to be done before i walk away.

I admit, I still havent found time to try your code. Will do soon.

Thanks,
gc