问题描述
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;