ASP.Net MVC 检查 aspnet 会员中用户的角色
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (Request.IsAuthenticated) {
%>
Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<br />
<% if(User.IsInRole("Administrator")) { %>
<br />
<%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %> | <%= Html.ActionLink("UserControl","UserControl","Account")%>
<% } else { %>
<br />
<%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %>
<%} %>
我想检查用户的角色(如果它是管理员),然后用户才能看到用户控件的链接。当我尝试运行此代码时,它给我一个错误: “‘Data.User’不包含‘IsInRole’的定义”
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (Request.IsAuthenticated) {
%>
Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<br />
<% if(User.IsInRole("Administrator")) { %>
<br />
<%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %> | <%= Html.ActionLink("UserControl","UserControl","Account")%>
<% } else { %>
<br />
<%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %>
<%} %>
I want to check the role of the user if it is an administrator only then the user can see the link to user control.when i try to run this code it is giving me an error saying
"'Data.User' does not contain a definition for 'IsInRole'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信 ViewUserControl 类型具有 User 属性,因此编译器认为您正在尝试访问 Data.User 类型的域对象。在部分控件中,您可以使用
HttpContext.Current.User
访问 User 实例,其中IsInRole
方法应该起作用。I don't believe the ViewUserControl type has a User property, so the compiler thinks you're trying to access your domain object of type Data.User. In a partial control, you can access the User instance using
HttpContext.Current.User
where theIsInRole
method should work.