2012-05-29 52 views
1

我嘗試使用下面的代碼獲取用戶的完整列表。但是我得到代碼「服務器無法聯繫。」從LDAP獲取用戶的完整列表

有什麼想法?

感謝,

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

更新:此代碼工作(從同一臺機器)

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: 
+0

你嘗試與您的開發機LDAP工具連接字符串? –

+0

請詳細說明。你可以使用LDAP工具瀏覽嗎?任何防火牆?等等...... – Reniuz

+0

查看我的更新1 –

回答

3

你的問題是,你的論點爲PrincipalConte xt不正確:您傳遞的是domainName中的LDAP查詢,而不是域控制器的名稱和端口。請參閱該類別的MSDN for full documentation

您的第二個代碼發佈是有效的,因爲您使用的類是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(); 
    } 
} 

希望幫助...

+0

在「PrincipalContext ctx」行上,幾秒鐘後出現此錯誤「未將對象引用設置爲對象的實例。」謝謝你的幫助。 –

+0

在此行上:PrincipalContext ctx = new PrincipalContext(ContextType.Domain,domainName); ?什麼是異常跟蹤? –

+0

是的,請參閱我的update2以獲取異常跟蹤。 –