我想针对AD执行LDAP查询以提取用户的位置(城市)。这就是我所说的:
public static string GetUserLocation(string userName)
{
string userLoc = "";
DirectoryEntry entry = new DirectoryEntry("LDAP://FTLAD04.corp.myDomain.com");
DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(&(objectClass=user)(l=" + userName + "))";
dSearch.PropertiesToLoad.Add("city");
SearchResult result = dSearch.FindOne();
userLoc = result.ToString();
entry.Close();
return userLoc;
}
我的SearchResult一直无效,任何人都可以帮我指出正确的方向吗?谢谢!
答案 0 :(得分:5)
我认为您的错误是您正在搜索该位置,但将用户名设置为值...
您应该搜索用户的名字 - 并获取该用户的位置:
public static string GetUserLocation(string userName)
{
string userLoc = "";
DirectoryEntry entry = new DirectoryEntry("LDAP://FTLAD04.corp.myDomain.com");
DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(&(objectClass=user)(samAccountName=" + userName + "))";
dSearch.PropertiesToLoad.Add("l");
SearchResult result = dSearch.FindOne();
if(result != null)
{
if(result.Properties["l"] != null && result.Properties["l"].Count > 0)
{
string location = result.Properties["l"][0].ToString();
}
}
return userLoc;
}
在AD中,用户的城市(您在Active Directory用户和计算机工具中输入的内容)存储在l
的{{1}}属性中。
有关所有属性的完整列表以及它们如何从ADU& C工具映射到实际的LDAP对象和属性,请参阅Robert Mueller's web site