There are several controls that allow you to display content based on the role a user is in, including:
- LoginView
- LoginStatus
And the web.config file allows us to control which pages can be viewed based on which role a user is in.
But what if you need to determine the role a user is in using the APIs? How do you do that?
It turns out that the API for this is really rather straightforward.
If you are in an ASPX or ASCX file, you can use
1 | if(User.IsInRole("roleNameHere")) |
If you are in other code where the User property is not available, you’ll need to use the HttpContext class like we’ve used previously this week to get access to the current context.
1 | if(HttpContext.Current.User.IsInRole("roleName")) |