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

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.

if(HttpContext.Current.User.IsInRole("roleName"))
{
  // do something here
}

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear