views:

33

answers:

1

I have some pages that have content that is relevant to both logged in users and non logged in users. For example, pages with contact information, privacy policies, etc. All the pages have your typical navigation menu but the thing is logged in users normally see a different navigation menu bar than non logged in users.

What is the best way to do this in ASP.net?

So far, possible solutions include the following:

  1. Displaying the content using a pop up window. The page will contain no menu and is just some basic page doesn't need to check what type of user is seeing it.
  2. Programmatically changing the master page depending on whether the user is authenticated or not. However, there are some variables on one of the master pages that need to be accessed but isn't touched at all by non logged in users.
  3. Putting the content in a user control and sticking this user control on two separate pages to be displayed to the appropriate user.

I'm not really a fan of #1 because users visiting the site for the first time may have some type of popup blocker or have javascript disabled.

I know #2 is possible by having the page use some type of base class that has inherited from MasterPage. However, I've read that this might not be the best design since now one of the pages has access to variables that isn't really necessary.

The third method sounds reasonable but then there'd be two separate ASPX files.

Is there a proper way of doing this? Or another method I haven't thought of yet?

edit

To clarify, logged in users need to set certain variables in their master pages where non logged in users do not. The reason for this is that there is a user control that displays a special navigation menu that will highlight certain items depending on these variables.

For example, the user control requires a string to determine which item to highlight. A page with profile information will provide "profile" as a parameter that will highlight the "Profile" item on the menu.

The menu in the user control is generated dynamically based on data from the database. The menu items are grouped by category and are displayed with an appropriate heading that is also pulled from the DB.

+1  A: 

Hey,

Programmably changing the master page is easy; just supply the correct URL on pre init, set

protected override void OnPreInit(..)
{
    if (this.User != null) {
       if (this.User.Identity.IsAuthenticated)
            this.MasterPageFile = "~/loggedin.master";
       else
            this.MasterPageFile = "~/notloggedin.master";
    }
}

No base class needed for this.

User control approach would work too, but changing master page file is really easy to do.

EDIT: If you have properties to set or get from the master, you could have the code-behind file implement the interface, and check if the this.Master reference is of that interface type.

HTH.

Brian
I forgot to point out that logged in users need to set certain variables in the master page (i.e. page title, etc.) where non logged in users do not. I've edited my question.
Michael
Well, do each master pages define its own set of parameters, or are they common across all X number of master pages?
Brian
I currently have 3 master pages. There is a base master page that contains common layout. There are also 2 nested master pages. One of these nested pages is designed for non logged in users and is strictly layout. The other is for logged in users and it requires certain parameters.The pages that I am thinking about are the ones that are displayed to both logged in and non logged in users. I don't really want to duplicate anything if possible. I'm wondering if method #2 might be the way to go here.
Michael
So you can switch the master page in PreInit based on logged in status, provided the pipeline sets the User property on the page at that time (not 100% sure), as in the updated code above.
Brian
Thanks. I'll give it a go.
Michael