Home » forms authentication » Determine The Role of a User in ASP.NET

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
}

 

Other post in forms authentication

Related Post

  • Forms Authentication – Manual AuthenticationForms Authentication – Manual Authentication I’ve had several occasions in the past where I’ve needed to do my own authentication or I’ve needed to add some additional methods to the authentication process. As easy as Microsoft […]
  • Setting Up Your Forms Based Authentication DatabaseSetting Up Your Forms Based Authentication Database I was recently asked if I would cover some topics related to Forms Based Authentication.  The person who requested this information has some specific issues that he wants covered […]
  • iTextSharp – HTML to PDF – Parsing HTMLiTextSharp – HTML to PDF – Parsing HTML Now that we have the HTML cleaned up, the next thing we will want to do is to parse the HTML. In my actual code for this, I parse the HTML and create the PDF at the same time, but for […]
  • Test Sending Email without a Server in ASP.NETTest Sending Email without a Server in ASP.NET By now, most people are familiar with the fact that ASP.NET will send mail from the codebehind by simply adding a few lines to your web.config file and adding another few lines of code in […]
  • JavaScript Boolean ConfusionJavaScript Boolean Confusion What could possibly be confusing about JavaScript Booleans you ask? Well, here’s several logical statements written in JavaScript.  Do you know what each does?     […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.