2010-06-02 6 views
1

J'essaie d'utiliser LDAP pour authentifier l'utilisateur, mais j'ai un problème avec LDAP.Le nom distinctif contient une erreur de syntaxe incorrecte

Ceci est mon code:

string hostOrDomainName = "MrHand-PC"; 
string targetOu = "cn=Huy Pham,ou=people,dc=example,dc=com"; 

// create a search filter to find all objects 
string ldapSearchFilter = "uid=pdhuy"; 

// establish a connection to the directory 
LdapConnection connection = new LdapConnection(hostOrDomainName); 

Console.WriteLine("\r\nPerforming a simple search ..."); 
SearchRequest searchRequest = new SearchRequest(targetOu, ldapSearchFilter, 
    System.DirectoryServices.Protocols.SearchScope.OneLevel, null); 

// cast the returned directory response as a SearchResponse object 
SearchResponse searchResponse = 
      (SearchResponse)connection.SendRequest(searchRequest); 

La dernière ligne déclenche une exception: The distinguished name contains invalid syntax.

Quelqu'un peut-il aider mon résoudre ce problème?

+1

Je ne pense pas que 'MrHand-PC' est un chemin LDAP valide pour LdapConnection - essayez d'utiliser quelque chose comme' LDAP: // MrHand-PC/dc = VotreCompagnie, dc = com' - un chemin LDAP ** valide ** –

+0

Merci pour la réponse rapide, je chemin utilisé: LDAP: // localhost: 389/dc = exemple, dc = com avec le navigateur LDAP et cela a fonctionné (j'installe OpenLDAP dans mon local PC). Tout est OK avec Active Directory, pouvez-vous expliquer mon problème? – handle0088

Répondre

2

Pour authentifier contre LDAP, vous pouvez essayer les éléments suivants (domaine, nom d'utilisateur et mot de passe sont des arguments):

bool IsAuthenticated = false;    
string domainAndUsername = domain + @"\" + username; 
string dirContext = GetAuthenticatingDirectory(domain); 
using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + dirContext, domainAndUsername, password)) 
{ 
    try 
    { 
     Object obj = entry.NativeObject; 
     DirectorySearcher search = new DirectorySearcher(entry); 
     search.Filter = "(SAMAccountName=" + username + ")"; 
     search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 
     if (result != null) 
     { 
      IsAuthenticated = true;        
     } 
    } 
    catch (Exception e) 
    { 
     //handle appropriately according to your requirements 
    } 
} 

return IsAuthenticated; 

où GetAuthenticatingDirectory() est définie comme

private string GetAuthenticatingDirectory(string domain) 
{ 
    string authenticatingDirectory = string.Empty; 
    string dotComDomain = domain + @".com"; 

    // Connect to RootDSE 
    using (DirectoryEntry RootDSE = new DirectoryEntry("LDAP://rootDSE")) 
    { 
     // Retrieve the Configuration Naming Context from RootDSE 
     string configNC = RootDSE.Properties["configurationNamingContext"].Value.ToString(); 

     // Connect to the Configuration Naming Context 
     using (DirectoryEntry configSearchRoot = new DirectoryEntry("LDAP://" + configNC)) 
     { 
      // Search for all partitions where the NetBIOSName is set. 
      using (DirectorySearcher configSearch = new DirectorySearcher(configSearchRoot)) 
      { 
       configSearch.Filter = ("(NETBIOSName=*)"); 

       // Configure search to return dnsroot and ncname attributes 
       configSearch.PropertiesToLoad.Add("dnsroot"); 
       configSearch.PropertiesToLoad.Add("ncname"); 
       using (SearchResultCollection forestPartitionList = configSearch.FindAll()) 
       { 
        // Loop through each returned domain in the result collection 
        foreach (SearchResult domainPartition in forestPartitionList) 
        { 
         // domainName like "domain.com". ncName like "DC=domain,DC=com" 
         string domainName = domainPartition.Properties["dnsroot"][0].ToString(); 
         string ncName = domainPartition.Properties["ncname"][0].ToString(); 

         if (dotComDomain.Equals(domainName, StringComparison.OrdinalIgnoreCase)) 
         { 
          authenticatingDirectory = ncName; 
          break; 
         } 
        } 
       } 
      } 
     } 
    } 

    return authenticatingDirectory; 
} 
+0

J'ai essayé votre code et j'ai reçu un message d'erreur: "le service d'annuaire n'est pas disponible". S'il vous plaît, aidez-moi à résoudre ce problème – handle0088

Questions connexes