Redirecting anonymous users

I had this problem. How do I keep the regular Sharepoint interface for internal users while forcing anonymous users to a separate part of the site?

Consider this… A site contains some data that the owner wants to publish to anonymous users. Cost prevents using the Publishing features, and in any case, the structure is far too complex so you need to publish regular Sharepoint data. You create a nice interface with pages that display the required data, including a custom master page and a lot of other customizations as well.

Now, you need to allow anonymous users access, right? However, if you do, customers can see the normal Sharepoint interface, allowing them to browse data outside the nice interface you have designed for them.

So you need to redirect them to a separate directory that houses the custom interface. And only if they are anonymous users, not if they are logged on users.

Here’s what I did:

  1. First, set up a normal site, and create an internet zone that allows anonymous access. While not strictly necessary for this solution, it is part of what I did.
  2. Put all your interface pages in a separate directory (ie /PublicPages)
  3. Create a custom control that checks to see if the user is anonymous, and if so, redirect them to that directory. Otherwise, do nothing.
  4. Put said custom control in you default.master.

Voila! Anonymous users will not gain access to the regular Sharepoint interface while your logged on users will get normal access.

Of course, there are a few quirks. Firstly, the custom control. I’ve pasted my source code below, feel free to use it as you please. If you need a binary, let me know and I can see what I can do for you.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AnonymousRedirect
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:AnonymousRedirect runat=server>")]
    public class AnonymousRedirect : WebControl
    {
        private string m_RedirectUrl;

        public string RedirectUrl
        {
            get { return m_RedirectUrl; }
            set { m_RedirectUrl = value; }
        }


        protected override void RenderContents(HtmlTextWriter output)
        {
            string name = Context.User.Identity.Name;
            if (name.Trim() == "")
            {
                Page.Response.Redirect(RedirectUrl, true);
            }
        }
    }
}

Compile and add to safecontrols.

Then, include the control in your default.master, something along the lines of:

<%@ Register TagPrefix="NittiTre" Assembly="NittiTre.Utilities.AnonymousRedirect, Version=0.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" Namespace="NittiTre.Utilities" %>

<%@ Register Tagprefix="AnonymousRedirect" Namespace="AnonymousRedirect" Assembly="AnonymousRedirect, Version=X.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXX" %>

and

and voila again, Bob’s your uncle.

The RedirectUrl can of course be customized. You need to fill in the Xs with your information. Look elsewhere for info on that, or let me know and I can post a follow up.

Found this article valuable? Want to show your appreciation? Here are some options:

a) Click on the banners anywhere on the site to visit my blog's sponsors. They are all hand-picked and are selected based on providing great products and services to the SharePoint community.

b) Donate Bitcoins! I love Bitcoins, and you can donate if you'd like by clicking the button below.

c) Spread the word! Below, you should find links to sharing this article on your favorite social media sites. I'm an attention junkie, so sharing is caring in my book!

Pin It