Get current Windows user?

Possible duplicate:
How to get user groups in Active Directory? (C #, asp.net)

Is there a way to get the current user using the System.DirectoryServices.AccountManagement namespace? I used 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 to get more information about the current user, for example the names of the groups to which they belong, in plain text, and wonders if this provides an AccountManagement namespace.

Using this, we simply return strings of numbers that I cannot understand:

 For Each item As IdentityReference In wi.Groups Console.WriteLine(item.ToString()) Next 
+4
source share
1 answer

Are you looking 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 google help on stackoverflow ;-) Active directory: get groups in which the user is a member

+5
source

All Articles