views:

147

answers:

4

Hi.

I have authenticated users to log on my system using this code:

FormsAuthentication.SetAuthCookie(user, false);

I want to hide my system menu for non authenticated users. Something like this:

<% if(???) {%>
   <ul id="menu>
      ...
   </ul>
<% } %>

How can I do this?

Thank you.

+8  A: 
if (Request.IsAuthenticated)

(This is how it's done in the default ASP.NET MVC template)

CD
Thank you! It's perfect to me.
MCardinale
+1  A: 

I think you want to use:

<% if(this.User.Identity.IsAuthenticated) { %>
<% } %>
Joel Potter
User and it's Identity can be null, so better to use `Request.IsAuthenticated`
mmcteam.com.ua
+2  A: 

if (Request.IsAuthenticated)

There's an example of this in the login user control of the basic mvc project.

if you want roles then

if (HttpContext.Current.User.IsInRole("myrole"))

Russell Steen
Thank you. I'm not using roles, but I appreciate to know this.
MCardinale
+1  A: 

I use: <% if( HttpContext.Current.User.Identity.IsAuthenticated ) %> or <% if( HttpContext.Current.User.Identity.IsInRole("roleName") ) %> but the other answers look like they'd also work fine.

Ben F