【发布时间】:2016-01-09 20:39:50
【问题描述】:
我在this question 中读到,在上述问题中也涵盖的 0x 错误的常见答案通常是指定要搜索的帐户。
我意识到我已经在这样做了——事实上,我正在尝试使用活动目录来对我的应用程序的用户进行身份验证。最初我认为我的错误问题与我如何制定搜索参数有关:
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
_path 返回了 URI。 domainAndUsername 返回以下内容:
"domain.com\\usrname"
所以我将实例化改为:
DirectoryEntry entry = new DirectoryEntry(_path, username, pwd);
返回“usr@domain.com”(我替换为“@domain.com”,因为我的实现需要用户输入他们的电子邮件地址和 NT 密码)。
这使我考虑到我的测试可能是在一个没有足够特权执行目录搜索的帐户下执行的可能性很小 - 但我已经使用另一个实现搜索了相同的数据 - 在同一个项目,不少。
那么,为什么每当我尝试使用object obj = entry.NativeObject; 时,我的实现会因0x80005000 而失败?
更重要的是,为什么 这个 实现会失败,而我原来的实现却没有?作为参考,此实现使用与 Microsoft here 演示的相同格式,我的原始(工作)实现如下:
public class dirSearch
{
public bool searchSuccessful;
public string errStr;
List<string> resList = new List<string>();
public void getEmpDetails(string filStr, string varStr)
{
string strServerDNS = "ldap.domain.com:389";
string strSearchBaseDN = "ou=People,o=domain.com";
string strLDAPPath = "LDAP://" + strServerDNS + "/" + strSearchBaseDN;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + filStr + ")";
//make sure the order of the search is like so:
//UID
//empnum
//full name
searcher.PropertiesToLoad.Add(varStr);
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties[varStr][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
}
更多信息:
我从异常中提取了错误代码,恰好是 -2147463168。这对应于ADS_BAD_PATHNAME。这有点令人困惑,因为 LDAP://ldap.domain.com:389/ 是正确的。我的理论是我应该使用专有名称,在这种情况下,我需要返回我原来的工作方法并提取工作人员的全名,然后再用该信息制定名称。
【问题讨论】:
标签: c# active-directory directoryservices