Search code examples
asp.netvb.netsecurityiisactive-directory-group

ASP.NET / VB.NET Check If a (different) User IsInRole


I have an ASP.NET application on our company's intranet. And a funky security requirement.

I need to check to see if a given username is in a certain role. I cannot use

Page.User.IsInRole("MyDomain\MyGroup")

because

Page.User.Identity.Name

Returns an empty string. Because of some lovely specifications for this program, I have to keep anonymous access enabled in IIS. Seems to rule out any page.user.identity stuff.

So I did find a way to (at least) get the current user (from System.Environment.UserName), but I need to bounce it against the domain group to see if they're in it. Or, better yet, get a list of users within a given domain so I can check myself. Something like...

Dim UserName as String

UserName = System.Environment.UserName

If User(UserName).IsInRole("MyDomain\MyGroup") Then
    MyFunction = "Success"
End If

-OR -

Dim GroupUsers as String()

GroupUsers = GetDomainUserNames("MyDomain\MyGroup")

Anybody have any ideas?


Solution

  • You can call IsUserInRole from the Roles static class. Here is a sample and some reference materials.

    Roles.IsUserInRole(username, rolename);
    

    link: http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.isuserinrole.aspx