如何在 C# .net 中从 Active Directory 中提取用户的城市位置

发布于 2024-12-13 13:14:46 字数 652 浏览 1 评论 0原文

我想对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2024-12-20 13:14:46

我认为您的错误是您正在搜索位置,但将用户名设置为值...

您应该搜索用户的名称 - 并获取该用户的位置:

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 用户和计算机工具中输入的城市)存储在 DirectoryEntryl 属性中。

有关所有属性的完整列表以及它们如何从 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:

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;
}

In AD, the user's City (that you enter in the Active Directory Users & Computers tool) is stored in the l attribute of the DirectoryEntry.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文