2011-11-07 6 views
-3

Active Directoryを使用しているWebアプリケーションがあります。 パラメータ(ユーザー、パスワード、および独自のドメイン)で認証ドメインを実行する関数を作成します。 determinatラインで は、以下の関数として、messagem「予期しないエラー」が表示されます。C#を使用しているActive Directoryで疑問がある

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

例外タイプとは何ですか? –

+0

エラーが指定されていません。 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) – FSouza

+1

*決して*新しい例外をスローしないでください( "ユーザーの認証エラー" + ex.Message)。 **常に**新しい例外をスローする( "Error authenticating user。"、ex) '。そうすれば、ラップされた例外に関するスタックトレース情報が失われることはありません。 –

答えて

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

これはおそらく、あなたのニーズに合わせてより良い仕事に行くされています。あなたは少なくとも.NET3.5が必要であり、あなたが認証に使うべきものです。

関連する問題