Search code examples
asp.net-mvcwindows-authentication

Roles available with Windows Authentication


I'm trying to add Roles authentication to an Action in a Controller in an ASP.NET MVC application. The code looks something like this:

[Authorize(Roles = "SomeRoleName")]
public ActionResult Index()
{
    bool inRole = User.IsInRole("Admin");

If I remove the Authorize attribute and put a breakpoint on the last line in that code sample, is there a way that I can inspect the objects and find out what roles are available?

e.g. I call User.IsInRole("Admin) in the Immediate window and it will give me a true/false value. How can I access the collection of roles available?


Solution

  • If you don't need to do this programatically, but you are trying to determine the correct Windows Groups/Roles that need to be specified, you can use this from the command line:

    C:\> net group /domain  (lists all Roles in the domain)
    C:\> net user <username> /domain (lists info, including roles for a user)
    

    Otherwise you will need to query the LDAP part of Active Directory, or use something under DirectoryServices.

    Take a look at these websites to access Active Directory via C#: