简体   繁体   中英

User.IsInRole error

I have this code inside the Page_Load of the Site.Master.cs .

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).

Any clues?

You can get current HttpContext user and validate for given role using IsInRole method as below.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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