2016-07-27 10 views
-1

Ich versuche LDAP-Verbindung mit C# zu erstellen.Erstellen einer LDAP-Verbindung unter .NET

ich diesen Server gefunden, die LDAP-Server gibt Test

http://www.forumsys.com/en/tutorials/integration-how-to/ldap/online-ldap-test-server/

Ich habe viel Post gegoogelt und versucht verbindet einen konsolidierten-Code

string domain = "ldap://ldap.forumsys.com/ou=mathematicians"; 
      string username = "cn=read-only-admin,dc=example,dc=com"; 
      string password = "password"; 
      string LdapPath = "Ldap://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com"; 


      string domainAndUsername = domain + @"\" + username; 
      DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password); 
      try 
      { 
       // Bind to the native AdsObject to force authentication. 
       Object obj = entry.NativeObject; 
       DirectorySearcher search = new DirectorySearcher(entry); 
       search.Filter = "(SAMAccountName=" + username + ")"; 
       search.PropertiesToLoad.Add("cn"); 
       SearchResult result = search.FindOne(); 

       // Update the new path to the user in the directory 
       LdapPath = result.Path; 
       string _filterAttribute = (String)result.Properties["cn"][0]; 
      } 
      catch (Exception ex) 
      { 

       throw new Exception("Error authenticating user." + ex.Message); 
      } 

Dieser Code erstellen nicht wird es geben unerwarteter Fehler ..

Ich habe auch einige andere Credentials versucht, aber sie helfen auch nicht ...

AUTH_LDAP_SERVER_URI = “ldap://ldap.forumsys.com” 
AUTH_LDAP_BIND_DN = “cn=read-only-admin,dc=example,dc=com” 
AUTH_LDAP_BIND_PASSWORD = “password” 
AUTH_LDAP_USER_SEARCH = LDAPSearch(“ou=mathematicians,dc=example,dc=com”, 
ldap.SCOPE_SUBTREE, “(uid=%(user)s)”) 

-------------------- 
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com'; 
$config[‘LDAP’][‘port’] = ‘389’; 
$config[‘LDAP’][‘user’] = ‘cn=read-only-admin,dc=example,dc=com'; 
$config[‘LDAP’][‘password’] = ‘password'; 

------------------------- 
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com/ou=mathematicians'; 
$config[‘LDAP’][‘port’] = ‘389’; 
$config[‘LDAP’][‘user’] = ‘gauss'; 
$config[‘LDAP’][‘password’] = ‘password'; 

-------------------------- 
OpenDSObject/GetObject functions, but don’t see a way to run a query with the ASDI objects. 
Set LDAP = GetObject(“LDAP:”) 
Set root = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389″, “cn=read-only-admin,dc=example,dc=com”, “password”, 0) 
Set ou = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389/ou=mathematicians,dc=example,dc=com””, “cn=read-only-admin,dc=example,dc=com”, “password”, 0) 
Set user = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389/uid=riemann,dc=example,dc=com”, “cn=read-only-admin,dc=example,dc=com”, “password”, 0) 

Ich brauche einen Vorschlag, was ich vermisse. Jede Ressource wird hilfreich sein.

+0

** Was ** unerwarteter Fehler? –

+0

Haben Sie versucht, das richtige Protokoll zu geben: LDAP statt Ldap oder ldap? Nach diesem http://forums.asp.net/t/1026497.aspx?LDAP+Problem+mit+NET+Unknown+error+0x80005000+ welches ich gerade googelte, kann das eine Ursache von Problemen sein. –

Antwort

0

Versuchen, PrincipalContext zu verwenden, um eine Verbindung zum LDAP-Server herzustellen. Hier ist ein guter wie-Artikel I verwiesen, als ich die ersten Schritte: http://ianatkinson.net/computing/adcsharp.htm

ctx = new PrincipalContext(
    ContextType.Domain, 
    "contoso.local", 
    "OU=Security Groups,OU=Contoso Inc,DC=contoso,DC=local", 
    "contoso\sysadmin", 
    "[email protected]"); 
1

Ich hatte ein etwas ähnliches Problem mit diesem Server und Google hat mich hierher geschickt.

Ein Problem, das ich sehe, ist die Groß-und Kleinschreibung in LDAP-Pfad. Außerdem sollten wir auch den AuthenticationType angeben.

Bitte überprüfen Sie den folgenden Codeblock, der funktionieren sollte.

string ldapServer = "LDAP://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com"; 
string userName = "cn=read-only-admin,dc=example,dc=com"; 
string password = "password"; 

var dirctoryEntry = new DirectoryEntry(ldapServer, userName, password, AuthenticationTypes.ServerBind); 

try { 
    object nativeObject = dirctoryEntry.NativeObject; 
    //Rest of the logic 
} catch (Exception ex) { 
    //Handle error 
}