可能重复:
如何获取Active Directory中的用户组? (c#,asp.net)
有没有办法使用System.DirectoryServices.AccountManagement
命名空间获取当前login的用户? 我一直在使用以下内容:
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)
但是我想了解更多关于当前用户的信息,比如他们所属的组名,并且想知道AccountManagement
命名空间是否提供了这个信息。
使用这只是返回我无法理解的数字string:
For Each item As IdentityReference In wi.Groups Console.WriteLine(item.ToString()) Next
你在寻找那样的东西吗?
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); } }
编辑:发现它与谷歌的一点点帮助在stackoverflow 😉 活动目录:获取用户所在的组