Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何为未经身份验证的用户隐藏我的菜单?_Asp.net Mvc_Authentication - Fatal编程技术网

Asp.net mvc 如何为未经身份验证的用户隐藏我的菜单?

Asp.net mvc 如何为未经身份验证的用户隐藏我的菜单?,asp.net-mvc,authentication,Asp.net Mvc,Authentication,我已验证用户使用以下代码登录我的系统: FormsAuthentication.SetAuthCookie(user, false); 我想为未经身份验证的用户隐藏我的系统菜单。大概是这样的: <% if(???) {%> <ul id="menu> ... </ul> <% } %> (在默认的ASP.NET MVC模板中就是这样做的)我想您应该使用: <% if(this.User.Identity.IsAu

我已验证用户使用以下代码登录我的系统:

FormsAuthentication.SetAuthCookie(user, false);
我想为未经身份验证的用户隐藏我的系统菜单。大概是这样的:

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


(在默认的ASP.NET MVC模板中就是这样做的)

我想您应该使用:

<% if(this.User.Identity.IsAuthenticated) { %>
<% } %>

如果(请求已验证)

在基本mvc项目的登录用户控件中有一个例子

如果你想要角色的话

如果(HttpContext.Current.User.IsInRole(“myrole”)

我使用:

<% if( HttpContext.Current.User.Identity.IsAuthenticated ) %>



但是其他答案看起来也不错。

谢谢。我不使用角色,但我很高兴知道这一点。用户及其标识可以为空,因此最好使用
Request.IsAuthenticated
<% if( HttpContext.Current.User.Identity.IsAuthenticated ) %>
<% if( HttpContext.Current.User.Identity.IsInRole("roleName") ) %>