4

Possible Duplicate:
How to get the groups of a user in Active Directory? (c#, asp.net)

Is there a way get the currently logged on user using the System.DirectoryServices.AccountManagement namespace? I've been using the following:

    Dim wi As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()

    Dim fullName As String = wi.Name.ToString

    Dim loggedOnUser = fullName.Substring(fullName.IndexOf("\") + 1)

    Console.WriteLine("The currently logged on user is {0}", loggedOnUser)

But I want more information about the current user, like the group names they belong to in plain text, and was wondering if the AccountManagement namespace provides this.

Using this just returns strings of numbers that I can't make sense of:

For Each item As IdentityReference In wi.Groups
    Console.WriteLine(item.ToString())
Next
Community
  • 1
  • 1
Cuthbert
  • 2,908
  • 5
  • 33
  • 60
  • 1
    "I want more information about the current user ": what kind of information? – Steve B Jan 04 '13 at 15:06
  • What kind of information do you want more? The WindowsIdentity-object returned from GetCurrent() contains more info than only a Name. http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx – Abbas Jan 04 '13 at 15:07
  • I'm sorry, I should have specified. I need to get the group names they belong to in plain text and I can't seem to figure out how to do that. – Cuthbert Jan 04 '13 at 15:08
  • http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.groups.aspx – snurre Jan 04 '13 at 15:19

1 Answers1

5

Are you searching for something like that?

using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "login");
    foreach (var group in user.GetGroups())
    {
        Console.WriteLine(group.Name);
    }
}

edit: found it with a little help of google at stackoverflow ;-) Active directory : get groups where a user is member

Community
  • 1
  • 1
jwillmer
  • 3,570
  • 5
  • 37
  • 73