views:

1186

answers:

3

Hi all,

I've posted a similar question about how to do this in JNDI, but in THIS post I am wondering how to do it using ANY INTERFACE.

I'd like to be able to retrieve a user's group name. When I say "group" I mean the group on the computer. Like "administrator" or "user."

Can I do it through the command line, application, dll, or interface of some sort?

Does anyone know how this might be done? Has anyone done this? Is it an easy task?

Your suggestions and experiences would help me greatly.

Thanks in advance (though I don't expect to get any reply)

Jbu

+3  A: 

From the command line:

net user <username>

or if they are on a domain

net user <username> /domain

Towards the bottom are 2 sections, Local Group Memberships and Global Group Memberships.

Note: a user may have alot of groups (in my case at work a total of 8!)

DrHazzard
Rock on. Great tip!
dpurrington
+1  A: 

Here's an example in VB (should be straightforward to convert to another language):

Dim User as IADsUser
Dim Group as IADsGroup
Dim UserDomain as String
Dim UserName as String

UserDomain = "Target_User_Domain"
UserName = "Target_User_Name"
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")

For Each Group in User.Groups
   Debug.Print Group.NameNext
Mitch Wheat
A: 

If you want to find out groups that the current logged in user is member of you can use

System.Security.Principal.WindowsIdentity.GetCurrent().Groups

UPDATE

If you want to get all of available groups, I think you have to use Windows APIs. But in order to check if current user is in a specific group, you can use the following code.

WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
bool isInGroup = principal.IsInRole("{Group Name}");

If you want to check against built-in windows groups you can use WindowsBuiltInRole enum in IsInRole method.

Mohammadreza
This only gets the SIDs (?) of a group and not the group names. Is there any way to use this info to get the group name?
jbu