.net - 获取当前登录的 Windows 用户?

标签 .net windows winforms

<分区>

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

有没有办法使用 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 的谷歌帮助下找到它 ;-) Active directory : get groups where a user is member

关于.net - 获取当前登录的 Windows 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159594/

相关文章:

c# - 在 Winforms web 浏览器控件中使用 ActiveXObject

.net - 反序列化时会发生什么?

Windows DataGridView BindingSource 索引超出范围异常

java - "Failed to attach to the remote VM"连接jdb到windows上的android模拟器

c# - 更改页眉高度/大小 Winform

c# - 在文本框中查看整个文件

c# - 使用带有 <%@ Register %> 的空 TagPrefix

c# - 如何在azure移动应用程序中对表 Controller 进行客户端调用

c# - 如何用 ReadOnlyCollection 之类的东西包装 int[,]?

c# - 尽管计算机进入待机模式,Window 应用程序能否继续运行?