2016-06-14 15 views
0

ユーザーにユーザー名とパスワードを使用してログインさせるWPFアプリケーションがあり、これはActive Directoryを通じて検証されます。テキストボックスからユーザー名を引き出し、アクティブなディレクトリにある姓、名字、電子メールでフィールドに入力したいと思います。ユーザー入力に基づいてActive Directoryからユーザー情報を取得

try 
{ 
    //enter AD settings 
    PrincipalContext AD = new PrincipalContext(ContextType.Domain, "LDAP://"); 

    //create search user and add criteria 
    string username = Email_box.Text; 

    UserPrincipal u = new UserPrincipal(AD); 
    u.GivenName = Email_box.Text; 

    //Search for user 
    PrincipalSearcher search = new PrincipalSearcher(u); 

    UserPrincipal result = (UserPrincipal)search.FindOne(); 
    search.Dispose(); 

    //show details in textboxes 
    Firstname_Text_Box.Text = u.GivenName; 
    Lastname_Text_Box.Text = u.Surname; 
} 
catch (Exception d) 
{ 
    Console.WriteLine("Error:" + d.Message); 
} 
+0

何が効いていないのですか? –

+0

テキストボックスにデータが入力されていません。 – EDraisey

+1

あなたはresult.GivenNameまたはu.GivenNameを使用していますか? – Ju66ernaut

答えて

1

私は、Active Directoryについて多くを知らないが、あなたは、テキストボックスを移入した結果を使用してすべきではありません。

これは私が試した何ですか?だからではなく、本の

Firstname_Text_Box.Text = u.GivenName; 
Lastname_Text_Box.Text = u.Surname; 

は、それはすべきではない:http://ianatkinson.net/computing/adcsharp.htm

+0

私はもともとresult.GivenNameを持っていて、それもうまくいきませんでした。 – EDraisey

+0

結果を見て、コードをステップ実行します。結果がnullの場合は、ユーザーを正しく検索していません。 Astxが言っていたことももう一つの問題だと思う。あなたのコードによると、あなたは電子メールアドレスでu.givennameを探しています。 UserPrincipalクラスを投稿して、すべてのメンバーを見ることができますか? – vgwizardx

0

前の回答に追加:

ここ
Firstname_Text_Box.Text = result.GivenName; 
Lastname_Text_Box.Text = result.Surname; 

私は例えば見ていたものですQueryFilterにも問題がある可能性があります。

これは、指定された名前が電子メールアドレスであると想定していることを示しています。

PrincipalContext AD = new PrincipalContext(ContextType.Domain, "LDAP://rootDSE"); 

それとも、以下で試すことができます。

0

たぶん、あなたはあなたの呼び出しで「でrootDSE」を逃しています。ユーザー名でADからユーザーのパスを取得します。パスを使用して情報を取得します。

string _Path = string.Empty; 
SearchResult results = null; 

using (var rootDse = new DirectoryEntry("LDAP://rootDSE")) 
     { 
      if (null != rootDse) 
      { 
       string path = string.Format("LDAP://{0}", rootDse.Properties["defaultNamingContext"][0]); 
       using (var de = new DirectoryEntry(path)) 
       { 
        using (var search = new DirectorySearcher(de) 
        { 
         Filter = "(&(objectClass=user)(sAMAccountName=username))" 
        }) 
        { 
         search.PropertiesToLoad.Add("DisplayName"); 
         search.PropertiesToLoad.Add("mail"); 

         results = search.FindOne(); 

         if (null != results) 
          _Path = results.Path; 
        }; 
       } 
      } 
     } 


using (var entry = new DirectoryEntry(_Path)) 
     { 
      using (var search = new DirectorySearcher(entry)) 
      { 
       return search.FindOne(); 
      } 
     } 
+0

元のコードにルートがあります。私はセキュリティの目的でそれを削除しました。 – EDraisey

関連する問題