views:

77

answers:

4

I have created a master page (Site.master) which contains the code to display a header, a footer, and a side bar. It works really great, but I am having trouble figuring out how to dynamically choose the header.

Basically, there are two possible header options. If the user is not logged in, I want them to see a login box and links for recovering their password, etc. If they are logged in, they'll see a logout link, and some information about their account (similar to how SO works, actually).

Is it possible to have the Site.master check and use whichever header I want depending on the login status of the user? I'm pretty stuck on where to begin with this (I thought maybe some checks in the code-behind of the master page) so any help would be appreciated.

+2  A: 

Yes, very easily by putting the two possible headers in their own Panel controls and just saying the following in the Page_Load:

if ( Request.IsAuthenticated )
{
    // Display
    pnlAuthenticated.Visible = true;
    pnlGuest.Visible = false;
} 
else 
{
    // Display
    pnlAuthenticated.Visible = false;
    pnlGuest.Visible = true;
}
Nissan Fan
Just a note, using a Panel will add <div> tags around the child controls which may interfere with the CSS. Placeholders are leaner in that respect as they will only render the Child Controls. However your solution will work fine.
Simon Mark Smith
There's no need to code this yourself. The `LoginView` control handles this by design, and provides a simple, declarative approach.
KP
A: 

Hey,

Yes two ways of doing it; embed the header in a panel, and show/hide the panel depending on login status (which occurs in code). Alternatively, you could use two master pages, and do this check in the OnPreInit method (or PreInit event handler) and switch to show which master page you want to use (you can only change master pages programmatically in this event handler).

The problem with the second option is that HttpContext.Current.user may not be available in PreInit...

HTH.

Brian
+1  A: 

Personally I would put each set of header controls into 2 different placeholders and by default set both to invisible

Then with some code in Master Page

PlaceHolder1.Visible = Context.User.IsAuthenticated
PlaceHolder2.Visible = !Context.User.IsAuthenticated
Simon Mark Smith
This won't work because on postback the user may no longer be authenticated and it would still show them the authenticated user's content. You need to implicitly handle it each time as per my example.
Nissan Fan
I have revised my example to be more concise and accurate
Simon Mark Smith
The `LoginView` control provides the needed functionality without extra code...
KP
+3  A: 

You should consider using the built-in control, LoginView (MSDN). It is specifically designed to provide multiple templates (views), for authenticated and anonymous users.

This is a best practice approach. You can define your headers/footers etc. for logged in and anonymous users, with appropriate login/logout buttons, user information, etc. etc.

Here's a very basic example:

<asp:LoginView id="LoginView1" runat="server">
    <AnonymousTemplate>
        <asp:HyperLink ID="lnkLogin" runat="server" NavigateUrl="~/Login.aspx" Text="Login"/>
    </AnonymousTemplate>
    <LoggedInTemplate>
        You are logged in as: <asp:LoginName id="lnCurrentUser" runat="server" />.
    </LoggedInTemplate>
</asp:LoginView>

The .NET framework will handle the rest, and display the correct template without any extra code. If you end up using multiple roles in your application you can take this one step further and define templates for these roles as well (administrator vs regular user, etc.)

A perfect solution to your question based on the above: How to: Display Different Information to Anonymous and Logged In Users

KP
Excellent answer, thank you! However (and I inherited this site), I'm guessing something may be wrong with how the site handles authentication as the control always seems to suggest that the user is logged in. Can you suggest where I might read about how to do correct authentication for the site within ASP.NET? I really don't think it's being done correctly right now.
JasCav
Glad it helped. In terms of ASP.NET authentication, the two most common built-in ways are (1) windows or (2) forms authentication. Unless you have the ability to authenticate your users against an Active Directory setup, go with forms authentication. Here's a description: http://msdn.microsoft.com/en-us/library/9wff0kyh.aspx. This article isn't bad either, although a little older: http://www.4guysfromrolla.com/webtech/110701-1.shtml
KP
Basically, in your web.config, you specify you are using forms authentication, you then deny anonymous users from either the root folder or sub folder (again using the web.config). The next step is to implement a very simple membership provider that asp.net will look to, for logging a user in, etc. The above articles explain this in far better detail than I can...
KP