Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将LDAP memberOf循环为返回,以显示单个用户的全部_C# - Fatal编程技术网

C# 将LDAP memberOf循环为返回,以显示单个用户的全部

C# 将LDAP memberOf循环为返回,以显示单个用户的全部,c#,C#,一般来说,我对使用LDAP和C#非常陌生,我做了不止几次搜索,但我尝试的大多数修复都没有结果 我正在从LDAP中提取信息。一切都正常,除了只有当我明确表示需要哪个数组号时,我才能提取memberOf information。试图使用foreach,或for语句的尝试毫无结果。我知道我可能错过了一些简单的东西,但我想我应该问问 public static String FindOther(String userAccount) { DirectoryEntry entry = GetDire

一般来说,我对使用LDAP和C#非常陌生,我做了不止几次搜索,但我尝试的大多数修复都没有结果

我正在从LDAP中提取信息。一切都正常,除了只有当我明确表示需要哪个数组号时,我才能提取memberOf information。试图使用
foreach
,或
for
语句的尝试毫无结果。我知道我可能错过了一些简单的东西,但我想我应该问问

public static String FindOther(String userAccount)
{
   DirectoryEntry entry = GetDirectoryEntry();
   DirectorySearcher search = new DirectorySearcher(entry);
   try
   {
      search.Filter = "(SAMAccountName=" + account + ")";
      search.PropertiesToLoad.Add("distinguishedName"); 
      search.PropertiesToLoad.Add("displayName"); 
      search.PropertiesToLoad.Add("mail"); 
      search.PropertiesToLoad.Add("CN");
      search.PropertiesToLoad.Add("Title");
      search.PropertiesToLoad.Add("sn");
      search.PropertiesToLoad.Add("givenname");
      search.PropertiesToLoad.Add("telephoneNumber");
      search.PropertiesToLoad.Add("memberOf"); 
      SearchResult result = search.FindOne();

      if (result != null)
      {
         return
            "Results for " + userAccount + "\n" +
            " DistinguishedName..: " + result.Properties["distinguishedName"][0].ToString() + "\n" +
            " Displayname........: " + result.Properties["displayname"][0].ToString() + "\n" +
            " eMail..............: " + result.Properties["mail"][0].ToString() + "\n" +
            " Common Name........: " + result.Properties["CN"][0].ToString() + "\n" +
            " Title..............: " + result.Properties["Title"][0].ToString() + "\n" +
            " Last Name..........: " + result.Properties["sn"][0].ToString() + "\n" +
            " First Name.........: " + result.Properties["givenname"][0].ToString() + "\n" +
            " Telephone..........: " + result.Properties["telephoneNumber"][0].ToString() + "\n" +                                    
            " Member Of..........: " + result.Properties["memberOf"][0].ToString() + "\n" +  
            " Member Of..........: " + result.Properties["memberOf"][1].ToString() + "\n" +  
            "End Transmission" + "\n";
         }
         else
         {
            return "Object not found... User ID: " + account;
         }
      }
      catch (Exception ex)
      {
         return "Big Ol Error: " + ex.Message + " User ID: " + account;
      }
   }

感谢您提供的所有帮助。

您可以通过PropertyCollection以以下方式进行枚举:


我将在这里给出一点免责声明,主要是因为我从未编写过Active Directory或轻量级目录访问协议的代码。我知道的一些事情是
DirectoryEntry
用法建议:

如果要查看实时条目,请使用GetDirectoryEntry 通过DirectorySearcher返回的条目的 要对返回的对象调用方法

此特定方法将直接从Active Directory返回信息。其中,
DirectorySearcher
将仅通过集合中当前可用的内容生成。我之所以提到这一点,是因为如果不填充集合,它将不会产生太多

我不确定你的整个目标,但我相信这就是你所追求的。如果不让我知道,我会修改代码

静态void Main(字符串[]参数) { string groupName=“域用户”; 字符串域名=”

这段代码实际上来自一本书,它被用来实现这样一个目标。但正如我在上面所说的,我从来没有实际完成过这样的任务——我只是希望为你们指明正确的方向


这太棒了,它将帮助我完成我需要的其他部分代码,但不是我想要的。不过,提供的另一个答案非常有效。不过,感谢您提供的资源!!没问题,很高兴这有帮助。一旦我足够聪明,将它放在搜索结果之后,在输入if(result!=null),并在返回中添加+Ret。非常感谢!!
   string Ret = string.Empty;
   ...
   foreach(object memberOf in result.Properties["memberOf"])
   {
      Ret += " Member Of..........: " + memberOf.ToString() + "\n";
   }
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);

    if (grp != null)
    {
         foreach (Principal p in grp.GetMembers(false))
            {
                Console.WriteLine(p.SamAccountName + " - " + p.DisplayName);
            }


        grp.Dispose();
        ctx.Dispose();
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
        Console.ReadLine();
    }
}