2011-11-07 5 views
-3

J'ai une application Web qui utilise Active Directory. Je crée une fonction pour effectuer le domaine d'authentification, dans le paramètre (Utilisateur, Mot de passe, et propre domaine). Conformément DÉTERMINAT affiche une Messagem « Erreur non spécifiée », comme la fonction ci-dessous:doutes à Active Directory en utilisant C#

public bool IsAuthenticated(string domain, string username, string pwd) 
    { 

     string domainAndUsername = domain + @"\" + username; 
     DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 

     try 
     { 
      //Bind to the native AdsObject to force authentication. 
      ---> This Line occurred error 
      **object obj = entry.NativeObject;** 
      ---> Line Above occurred error 

      DirectorySearcher search = new DirectorySearcher(entry); 

      search.Filter = "(SAMAccountName=" + username + ")"; 
      search.PropertiesToLoad.Add("cn"); 
      SearchResult result = search.FindOne(); 

      if (null == result) 
      { 
       return false; 
      } 

      //Update the new path to the user in the directory. 
      _path = result.Path; 
      _filterAttribute = (string)result.Properties["cn"][0]; 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error authenticating user. " + ex.Message); 
     } 

     return true; 
    } 
+3

Quel est le type d'exception? –

+0

Erreur non spécifiée dans System.DirectoryServices.DirectoryEntry.Bind (Boolean throwIfFail) – FSouza

+1

* Ne jamais lancer une nouvelle exception ("Erreur lors de l'authentification de l'utilisateur." + Ex.Message) ". ** Toujours ** lance une nouvelle exception ("Erreur d'authentification de l'utilisateur.", Ex) '. De cette façon, vous ne perdez jamais les informations de trace de la pile sur votre exception enveloppée. –

Répondre

1
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")) 
{ 
    // validate the credentials 
    bool isValid = pc.ValidateCredentials("myuser", "mypassword") 
} 

Cela va probablement mieux travailler pour vos besoins. Vous avez besoin de .NET3.5 au moins et c'est ce que vous devriez utiliser pour vous authentifier.