quot;A referral was returned from the serverquot; exception when accessing AD from C#(“从服务器返回了一个推荐从 C# 访问 AD 时的异常)
问题描述
DirectoryEntry oDE = new DirectoryEntry("LDAP://DC=Test1,DC=Test2,DC=gov,DC=lk");
using (DirectorySearcher ds = new DirectorySearcher(oDE))
{
    ds.PropertiesToLoad.Add("name");
    ds.PropertiesToLoad.Add("userPrincipalName");
    ds.Filter = "(&(objectClass=user))";
    SearchResultCollection results = ds.FindAll();
    foreach (SearchResult result in results)
    {
        Console.WriteLine("{0} - {1}",
            result.Properties["name"][0].ToString(),
            result.Properties["userPrincipalName"][0].ToString());
    }
}
在 SearchResultCollection results = ds.FindAll(); 行我得到一个异常:
On the SearchResultCollection results = ds.FindAll(); line I get an exception:
从服务器返回了一个引用
A referral was returned from the server
为什么我会收到该异常,这意味着什么?
Why do I get that exception and what does it mean?
推荐答案
这是问题的答案.原因是我的 LDAP 字符串错误.
This is the answer for the question.Reason for the cause is my LDAP string was wrong.
    try
    {
        string adServer = ConfigurationManager.AppSettings["Server"];
        string adDomain = ConfigurationManager.AppSettings["Domain"];
        string adUsername = ConfigurationManager.AppSettings["AdiminUsername"];
        string password = ConfigurationManager.AppSettings["Password"];
        string[] dc = adDomain.Split('.');
        string dcAdDomain = string.Empty;
        foreach (string item in dc)
        {
            if (dc[dc.Length - 1].Equals(item))
                dcAdDomain = dcAdDomain + "DC=" + item;
            else
                dcAdDomain = dcAdDomain + "DC=" + item + ",";
        }
        DirectoryEntry de = new DirectoryEntry("LDAP://" + adServer + "/CN=Users," + dcAdDomain, adUsername, password);
        DirectorySearcher ds = new DirectorySearcher(de);
        ds.SearchScope = SearchScope.Subtree;
        ds.Filter = "(&(objectClass=User)(sAMAccountName=" + username + "))";
        if (ds.FindOne() != null)
            return true;
    }
    catch (Exception ex)
    {
        ExLog(ex);
    }
    return false;
                        这篇关于“从服务器返回了一个推荐"从 C# 访问 AD 时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“从服务器返回了一个推荐"从 C# 访问 AD 时的
				
        
 
            
        - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 
						
						
						
						
						