Dave's Notebook

Determine The Role of a User in ASP.NET

winter-016

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
2
3
4
if(User.IsInRole("roleNameHere"))
{
// do something here
}

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
2
3
4
if(HttpContext.Current.User.IsInRole("roleName"))
{
// do something here
}