简体   繁体   English

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

[英]Get distinguished name from Active Directory of currently logged in user

如何从C#中当前登录用户的Active Directory中获得专有名称?

Check following snippet. 检查以下代码段。 You have pass to Identity.Name from IPrincipal . 您已从IPrincipal传递给Identity.Name I assume that the user is already authenticated in Active Directory (ie. using standard IIS authorization methods). 我假设用户已经在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;
}        

您为什么不只使用: System.DirectoryServices.AccountManagement.UserPrincipal.Current.DistinguishedName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM