.net 是否获取当前登录的Windows用户?
可能重复:.net 是否获取当前登录的Windows用户?,.net,windows,winforms,.net,Windows,Winforms,可能重复: 是否有方法使用System.DirectoryServices.AccountManagement命名空间获取当前登录的用户?我一直在使用以下方法: Dim wi As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent() Dim fullName As String = wi.Name.ToString Dim
是否有方法使用
System.DirectoryServices.AccountManagement
命名空间获取当前登录的用户?我一直在使用以下方法:
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
名称空间是否提供了这些信息
使用它只返回我无法理解的数字字符串:
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上通过谷歌的一点帮助找到了它;-)
“我想要更多关于当前用户的信息”:什么样的信息?你想要更多什么样的信息?从GetCurrent()返回的WindowsIdentity对象包含的信息不仅仅是名称。对不起,我应该说明的。我需要以纯文本的形式获取它们所属的组名,但我似乎不知道如何做到这一点。