c# - 从当前登录用户的 Active Directory 中获取专有名称

标签 c# .net active-directory distinguishedname

如何在 C# 中从 Active Directory 中获取当前登录用户的可分辨名称?

最佳答案

检查以下代码段。您已从 IPrincipal 传递给 Identity.Name .我假设用户已经在 Active Directory 中进行了身份验证(即使用标准 IIS 授权方法)。

private string GetUserName(string identity)
{
    if (identity.Contains("\\"))
    {
        string[] identityList = identity.Split('\\');
        return identityList[1];
    }
    else
    {
        return identity;
    }
}

public string GetUserDn(string identity)
{            
    var userName = GetUserName(identity);   
    using (var rootEntry = new DirectoryEntry("LDAP://" + adConfiguration.ServerAddress, null, null, AuthenticationTypes.Secure))
    {       
        using (var directorySearcher = new DirectorySearcher(rootEntry, String.Format("(sAMAccountName={0})", userName)))
        {
            var searchResult = directorySearcher.FindOne();                    
            if (searchResult != null)
            {
                using (var userEntry = searchResult.GetDirectoryEntry())
                {
                    return (string)userEntry.Properties["distinguishedName"].Value;                 
                }
            }
        }                
    }   
    return null;
}        

关于c# - 从当前登录用户的 Active Directory 中获取专有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428495/

相关文章:

c# - 尝试了解类型转换和装箱/拆箱

c# - 是否可以有两个带有透明图像的重叠 PictureBox 控件?

azure - 未找到领域(ACS、ADDS、AD FS)

c# - 从 Active Directory 获取 GUID 或 native GUID

c# - Azure 函数在使用设备客户端 SDK 发送 D2C 消息时引发错误

c# - 如何使用正则表达式从框架内定义的输入字符串中获取字符串

c# - DevExpress GridControl 行号

c# - 使用 .NET 开发新的应用程序?

c# - 如何防止后台线程中的异常终止应用程序?

c# - 如何确保 IsInRole 检查不使用缓存的凭据