如何在 C# .net 中从 Active Directory 中提取用户的城市位置
我想对 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 不断返回 null,任何人都可以帮助我指出正确的方向吗?谢谢你!
I'd like to do a LDAP query against AD to pull a user's location (city). This is what I've put together:
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;
}
My SearchResult keeps coming back null, can anyone help point me in the right direction? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的错误是您正在搜索位置,但将用户名设置为值...
您应该搜索用户的名称 - 并获取该用户的位置:
在 AD 中,用户的城市(您在 Active Directory 用户和计算机工具中输入的城市)存储在
DirectoryEntry
的l
属性中。有关所有属性的完整列表以及它们如何从 ADU&C 工具映射到实际 LDAP 对象和属性,请参阅 罗伯特·穆勒的网站
I think your error is that you're searching for the location, but setting the user name as the value...
You should search for the user's name - and grab the location for that user:
In AD, the user's City (that you enter in the Active Directory Users & Computers tool) is stored in the
l
attribute of theDirectoryEntry
.For a complete list of all attributes and how they map from the ADU&C tool to actual LDAP objects and attributes, see Robert Mueller's web site