简体   繁体   English

User.IsInRole错误

[英]User.IsInRole error

I have this code inside the Page_Load of the Site.Master.cs . 我在Site.Master.csPage_Load中有这个代码。

if(User.IsInRole("Read"))
{
   NavigationMenu.Visible = false;
}

and I get this error: 我收到此错误:

An object reference is required for the non-static field, method, or property 'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string). 非静态字段,方法或属性'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string)需要对象引用。

Any clues? 有线索吗?

You can get current HttpContext user and validate for given role using IsInRole method as below. 您可以使用IsInRole方法获取当前的HttpContext 用户并验证给定角色,如下所示。

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

Change your method as 将您的方法更改为

if(HttpContext.User.IsInRole("Read"))
{
   NavigationMenu.Visible = false;
}

Looks like you are using the class instead an instance of that class try: 看起来你正在使用该类而不是该类的实例尝试:

User user = new User();
user.IsInRole("Read");

I don't fully agree with the answer. 我不完全同意答案。 You are getting the User instance from the Page property in your master page, so you should use: 您正在从母版页的Page属性中获取User实例,因此您应该使用:

var user = Page.User;
user.IsInRole("your role");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM