Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show menu item only for logged-on users

Tags:

asp.net-mvc

I'm new to ASP.NET MVC and am using version 1.0 of the framework. I have a site.master page with the following hard-coded menu

<div id="menucontainer">
    <ul id="menu">              
    <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
    <li><%= Html.ActionLink("Drivers", "List/?category=Drivers", "Product")%></li>
    <li><%= Html.ActionLink("Irons", "List/?category=Irons", "Product") %></li>
    <li><%= Html.ActionLink("Wedges", "List/?category=Wedges", "Product") %></li>
    <li><%= Html.ActionLink("Putters", "List/?category=Putters", "Product") %></li> 
    </ul>  
 </div>

I want to show an extra item on the menu only if users are logged on. Something like "View My Listings". I have no problem doing this in a dirty hacky way so I have tried

<% if (User.Identity.IsAuthenticated) ...

but User is not valid in this context. My question is how to show an extra menu item only if users are logged on?

like image 846
Peter Kelly Avatar asked Jul 16 '10 21:07

Peter Kelly


2 Answers

For MVC 4+ We do like this:

@if(Context.User.Identity.IsAuthenticated) {
     <li> @Html.ActionLink(... 
   }
like image 93
Roger Avatar answered Nov 07 '22 08:11

Roger


I also just discovered that I could use

<% if (Request.IsAuthenticated) { %>
  <li><%= Html.ActionLink("View my Listings", "MyListings", "List")%>
<% } %>
like image 40
Peter Kelly Avatar answered Nov 07 '22 09:11

Peter Kelly