1

I try to use the code below to get the full list of user. But I get the code "The server could not be contacted."

Any idea ?

Thanks,

static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "LDAP://ldap.mycompany.be:389/ou=users,o=mycompany,dc=mycompany,dc=be";

    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(String.Format("{0} - {1}", 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();
    }
}

Update : This code is working (from the same machine)

static void Main(string[] args)
{
    string userUid = "myuser";


    DirectoryEntry Ldap = new DirectoryEntry("LDAP://ldap.mycompany.be:389/ou=users,o=mycompany,dc=mycompany,dc=be", "", "", AuthenticationTypes.Anonymous);
    DirectorySearcher LdapSearcher = new DirectorySearcher(Ldap, String.Format("(&(objectClass=*)(uid={0}))", userUid));


    LdapSearcher.PropertiesToLoad.Add("cn");
    LdapSearcher.PropertiesToLoad.Add("uid");
    LdapSearcher.PropertiesToLoad.Add("mail");
    LdapSearcher.PropertiesToLoad.Add("employeeNumber");
    LdapSearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");
    LdapSearcher.PropertiesToLoad.Add("foremfunction");
    LdapSearcher.PropertiesToLoad.Add("foremservice");
    LdapSearcher.PropertiesToLoad.Add("foremsite");
    LdapSearcher.PropertiesToLoad.Add("inetUserStatut");
    LdapSearcher.PropertiesToLoad.Add("telephoneNumber");
    LdapSearcher.PropertiesToLoad.Add("uid");
    LdapSearcher.PropertiesToLoad.Add("mail");
    SearchResultCollection LdapSearcherResults = LdapSearcher.FindAll();

    foreach (SearchResult resultLdap in LdapSearcherResults)
    {
        Console.WriteLine(resultLdap.Properties["cn"][ 0].ToString());
        Console.WriteLine(resultLdap.Properties["uid"][0].ToString());
        Console.WriteLine(resultLdap.Properties["mail"][0].ToString());
    }
}

Update2

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=System.DirectoryServices.AccountManagement
  StackTrace:
       at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
       at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
       at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
       at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name)
       at MoulinetteUser.Program.Main(String[] args) in C:\Users\.....\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
4

1 回答 1

3

您的问题是 PrincipalContext 的参数不正确:您在 domainName 中传递 LDAP 查询,而不是域控制器的名称和端口。有关该类的完整文档,请参阅MSDN 。

您的第二个代码帖子有效,因为您使用的类是 LDAP 客户端类,它“理解”您的 ldap 查询。

尝试以下方法,看看它是否有效:

static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "ldap.mycompany.be"; // or whatever your domain controller's name is...

    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(String.Format("{0} - {1}", 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();
    }
}

希望有帮助...

于 2012-05-29T11:52:48.913 回答