【问题标题】:Connecting to Active Directory to retrieve user details using C#使用 C# 连接到 Active Directory 以检索用户详细信息
【发布时间】:2011-04-22 08:14:56
【问题描述】:

我发现一篇关于从here获取活动目录中的用户的文章

所以可能我的代码会喜欢这个

String strPath = "format of this path";
DirectoryEntry entry = null;
entry = new DirectoryEntry(strPath);

DirectorySearcher mySearcher = new DirectorySearcher(entry);

mySearcher.Filter = ("ObjectCategory=user");

foreach (SearchResult result in mySearcher.FindAll())
{
    String strName = result.GetDirectoryEntry().Name;
    //Do whatever
}

你能解释一下这里的strPath吗?这是什么格式??

注意:我知道我的服务器信息可以使用我的本地 ip "198.168.1.182" 获取,用于测试目的。

我不确定我的想法是否正确。

请帮忙!!!

【问题讨论】:

    标签: visual-studio-2010 c#-4.0 active-directory


    【解决方案1】:

    由于您使用的是 .NET 4,因此您绝对应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    Managing Directory Security Principals in the .NET Framework 3.5

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find user by name
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "John Doe");
    
    // if we found user - inspect its details
    if(user != null)
    {
        string firstName = user.GivenName;
        string lastName = user.Surname;
        string email = user.EmailAddress;
        string phone = user.VoiceTelephoneNumber;
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易:

    【讨论】:

    • marc_s 的回答不完整。 FindyByIntenty 方法至少需要两个参数。主要上下文对象“ctx”需要是第一个参数。
    • @jdmorris:谢谢,你说得对 - 更正了我的回答
    • 非常感谢。我整天都在寻找解决方案。这使得它非常简单易用。
    【解决方案2】:

    strPath 是 LDAP URL,更多信息可以在这里找到: http://en.wikipedia.org/wiki/LDAP#LDAP_URLs

    这个结构一开始可能看起来有点奇怪,但维基百科上的例子给出了一个很好的总结。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 2020-03-13
      相关资源
      最近更新 更多