目前有个项目通过域验证,用户访问该网站,需要显示其显示名,不是登录名。但是始终没有成功,代码如下:
// 取得域路径
string strADServerPath = System.Configuration.ConfigurationManager.AppSettings["ADServerPath"];
System.DirectoryServices.DirectoryEntry jp3LdapEntry = new System.DirectoryServices.DirectoryEntry(strADServerPath);
// 显示名(displayname)
 string EnglishName = null;
// 域登陆名(alias) 
        string userName = "test";
        System.DirectoryServices.SearchResult r = null;
        System.DirectoryServices.DirectorySearcher dsearch = new System.DirectoryServices.DirectorySearcher(jp3LdapEntry);
        dsearch.Filter = "(&(&(|(&(objectCategory=person)(objectSid=*)(!samAccountType:1.2.840.113556.1.4.804:=3))(&(objectCategory=person)(!objectSid=*))(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=14)))(objectCategory=user)(mailNickname=" + userName + ")))";        try
        {
            r = dsearch.FindOne();
            if (r != null)
            {
                EnglishName = r.GetDirectoryEntry().Properties["cn"].Value.ToString();
            }
            else
            {
                EnglishName = null;
            }
        }
        catch
        {
            EnglishName = null;
        }
 r = dsearch.FindOne(); 这个地方始终返回null值,不知道为什么?请各位指教。