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 时的
基础教程推荐
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 创建属性设置器委托 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01