Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is there a way to load a different master page if I can identify the user to be annonymous?

share|improve this question
    
In my experience the "master page switching" approach leads to a great deal of confusion and complexity. I would try to avoid it if you can find another way of achieving what you are looking for. –  SPDoctor Nov 11 '11 at 0:12
    
any accepted answer? –  Yuri Leontyev Nov 12 '11 at 21:36
add comment

8 Answers

up vote 12 down vote accepted

If you want to do it at page level, you can assign a different master page on "OnPreInit" event. Below is the example to do it on layout page if you have publishing site :

public class MyPublishingLayoutPage:PublishingLayoutPage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            if (Context.Request.LogonUserIdentity.IsAnonymous)
            {                
              SPWeb web = SPContext.Current.Web;
              this.MasterPageFile = "~/_catalogs/masterpage/anonymous.master";                   

            }
        }

    }

If you have many pages, I would recommend to use HttpModule to hook PreInit handler into the page. Below is the example :

public class AnonymousMasterModule: IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute+= new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
                string requestUrl = HttpContext.Current.Request.Url.ToString();
                if (!requestUrl.Contains(".aspx"))
                    return;               

                 Page page = HttpContext.Current.Handler as Page;
                 if (page != null)
                    page.PreInit += new EventHandler(page_PreInit);                

        }

        void page_PreInit(object sender, EventArgs e)
        {            
            Page page = sender as Page;
            if (page != null)
            {
                if (Context.Request.LogonUserIdentity.IsAnonymous)
                {
                    SPWeb web = SPContext.Current.Web;
                    page.MasterPageFile = "~/_catalogs/masterpage/anonymous.master";
                }
            }

        }
        public void Dispose()
        {

        }
    }
share|improve this answer
add comment

You can use an Http Module to accomplish this.

share|improve this answer
add comment

If your goal is to have a different look and feel for different users, you can use a single Master Page, and include steps that only target specific profiles (anonymous users in your case).

Search the internet for the following terms: SPSecurityTrimmedControl and AnonymousTemplate.

It is what I use on this draft page:

http://usermanagedsolutions.com

For authenticated users, the site is just an out of the box SharePoint 2010 page. I add a css layer for anonymous users to modify the page width and hide the top bar.

share|improve this answer
add comment

This post describes how can you change masterpage dynamiccally.

share|improve this answer
    
This is sharepoint 2007, so I don't have access to code behind like that. Is there a way to do it 2007? –  mdrussell0779 Nov 10 '11 at 18:37
    
sorry, I don't know another solution. –  Alexander Nov 10 '11 at 18:47
    
If you working with SharePoint 2007, you can create HttpModule, and change the master page as described in the post. I don't see a reason related to SP2007 why you can't do that if you have SP on dedicated server. We used this approach to customize design of application pages in SP2007. Steps: –  Yuri Leontyev Nov 10 '11 at 19:58
    
Steps: 1. create HttpModule that performs changing master page for anonymous users. 2. deploy it in GAC 3. register Module in web.config - Thats all. –  Yuri Leontyev Nov 10 '11 at 20:03
    
@mdrussell0779 : You can create a code behind and implement what Alexander has suggested. –  Amit Kumawat Nov 11 '11 at 10:44
add comment

You can use two options only for targeting content to users (not change of master page):

  1. Audience for Server version - http://www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c13255__2/ and for 2010 but the concept seems to be the same in 2010 - http://blog.mastykarz.nl/content-targeting-anonymous-users-sharepoint-server-2010-part2-2/
  2. SPSecurityTrimmedControl - which allows showing content based on permissions - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spsecuritytrimmedcontrol%28v=office.12%29.aspx
share|improve this answer
add comment

You can include within the Master Page steps that only target specific profiles (anonymous users in your case).

Search the internet for the following terms: SPSecurityTrimmedControl and AnonymousTemplate.

It is what I use on this draft page:

http://usermanagedsolutions.com

For authenticated users, the site is just an out of the box SharePoint 2010 page. I add a css layer for anonymous users to modify the page width and hide the top bar.

share|improve this answer
add comment

This is the same what you asked, See the answer: A seperate masterpage for annoymous users?

You can do with httpmodule or a code-behind in a page

share|improve this answer
add comment

I have tried your example in my publishing site for web part pages. But its not work.

i debug the code but event handler is not call the below method.

void page_PreInit(object sender, EventArgs e)

Please advice...

Thanks in advance

share|improve this answer
    
Did you register the HttpModule in web.config? Google up "HttpModule" and how to use it. –  Sleeper Smith Oct 19 '12 at 3:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.