User.IsInRole错误

pik*_*ikk 2 c# asp.net authorization

我里面的这段代码Page_LoadSite.Master.cs.

if(User.IsInRole("Read"))
{
   NavigationMenu.Visible = false;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

非静态字段,方法或属性'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string)需要对象引用.

有线索吗?

Dam*_*ith 7

您可以使用IsInRole方法获取当前的HttpContext 用户并验证给定角色,如下所示.

HttpContext.Current.User.IsInRole("Read")

将您的方法更改为

if(HttpContext.User.IsInRole("Read"))
{
   NavigationMenu.Visible = false;
}
Run Code Online (Sandbox Code Playgroud)