【问题标题】:User.IsInRole errorUser.IsInRole 错误
【发布时间】:2011-08-20 14:43:02
【问题描述】:
我在Site.Master.cs 的Page_Load 中有这段代码。
if(User.IsInRole("Read"))
{
NavigationMenu.Visible = false;
}
我得到这个错误:
非静态字段、方法或
财产
'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string).
有什么线索吗?
【问题讨论】:
标签:
c#
asp.net
authorization
【解决方案1】:
您可以获得当前的HttpContextuser 并使用IsInRole method 验证给定角色,如下所示。
HttpContext.Current.User.IsInRole("Read")
改变你的方法为
if(HttpContext.User.IsInRole("Read"))
{
NavigationMenu.Visible = false;
}
【解决方案2】:
看起来您正在使用该类而不是该类的实例尝试:
User user = new User();
user.IsInRole("Read");
【解决方案3】:
我不完全同意这个答案。您正在从母版页中的 Page 属性获取 User 实例,因此您应该使用:
var user = Page.User;
user.IsInRole("your role");