2

メールを送信できるアプリケーションがあります。ldap ここで、を使用してユーザーの電子メールを認証するように求められます。この概念は非常に新しいものです。ldapサーバーリンクが与えられました。それをどのように進めるかわかりません。どんな記事やヒットも大いに役立ちます。

これが私が試しているコードです

public static UserDetail GetUserDetails(string EmailId, string domainName)
{
    UserDetail userDetail = new UserDetail();

    try
    {
        string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", EmailId);
        string[] properties = new string[] { "fullname" };

        DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domainName, null, null, AuthenticationTypes.Secure);
        DirectorySearcher searcher = new DirectorySearcher(adRoot);
        searcher.SearchScope = SearchScope.Subtree;
        searcher.ReferralChasing = ReferralChasingOption.All;
        searcher.PropertiesToLoad.AddRange(properties);
        searcher.Filter = filter;
        SearchResult result = searcher.FindOne();

        DirectoryEntry directoryEntry = result.GetDirectoryEntry();
        string displayName = directoryEntry.Properties["displayName"[0].ToStrin();
        string firstName = directoryEntry.Properties["givenName"][0].ToString();
        string lastName = directoryEntry.Properties["sn"][0].ToString();
        string emailId = directoryEntry.Properties["mail"][0].ToString();

        userDetail.EmailId = emailId;
    }
    catch (Exception)
    {

    }
    return userDetail;
}

検索ボタンをクリックするだけで実現したい。メソッドを呼び出して変数を渡すにはどうすればよいですか。

4

3 に答える 3

3

.NET 3.5以降を使用している場合は、PrincipalSearcherおよび「例によるクエリ」プリンシパルを使用して検索を実行できます。

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the e-mail of "bruce@example.com"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.EmailAddress = "bruce@example.com";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// try to find that user
UserPrincipal found = srch.FindOne() as UserPrincipal;

if(found != null)
{
    // do whatever here - "found" is the user that matched the e-mail given
}
else
{
    // there wasn't any user with that e-mail address in your AD
}

まだ読んでいない場合は、MSDNの記事「.NETFramework3.5でのディレクトリセキュリティプリンシパルの管理」を絶対に読んでください。この記事では、の新機能を最大限に活用する方法を説明していますSystem.DirectoryServices.AccountManagement。または、System.DirectoryServices.AccountManagement名前空間に関するMSDNドキュメントを参照してください。

もちろん、必要に応じて、作成する「例によるクエリ」ユーザープリンシパルに他のプロパティを指定することもできます。

  • DisplayName(通常:名+スペース+姓)
  • SAM Account Name-Windows/ADアカウント名
  • User Principal Name-「username@yourcompany.com」スタイル名

の任意のプロパティを指定して、UserPrincipalそれらをの「例によるクエリ」として使用できますPrincipalSearcher

于 2012-10-18T08:43:24.090 に答える
1

emailAddress(タイプ文字列)の入力が与えられると、このコードはLDAPディレクトリで一致する電子メールアドレスを持つユーザーを検索し、ユーザーに関する情報を返します。

string fullName = string.Empty;
            string givenName = string.Empty;
            string distinguishedName = string.Empty;
            string sAMAccountName = string.Empty;
            using (var context = new PrincipalContext(ContextType.Domain, "DOMAIN"))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
                {
                    foreach (Principal result in searcher.FindAll())
                    {
                        var de = result.GetUnderlyingObject() as DirectoryEntry;

                        if (de.Properties["cn"].Value.ToString().Contains(" "))
                        {

                            //var userEntry = new DirectoryUser(de.Properties["sAMAccountName"].Value.ToString());
                            var currentUserEmail = de.Properties["mail"].Value.ToString().ToLower();
                            if (currentUserEmail == emailAddress)
                            {

                                if (de.Properties["cn"].Value != null)
                                    fullName = de.Properties["cn"].Value.ToString();
                                if (de.Properties["givenName"].Value != null)
                                   givenName = de.Properties["givenName"].Value.ToString();
                                if (de.Properties["distinguishedName"].Value != null)
                                    distinguishedName =de.Properties["distinguishedName"].Value.ToString();
                                if (de.Properties["sAMAccountName"].Value != null)
                                    sAMAccountName = de.Properties["sAMAccountName"].Value.ToString();


                            }
                        }
                    }
                }
            }

以下への参照が必要です:

System.DirectoryServices;
System.DirectoryServices.AccountManagement;

私が言及したい1つの注意点は、ディレクトリ検索ルーチンが非常に遅くなる可能性があるということです。ドメインに100,000人のユーザーがいる場合、このプロセスの実行には時間がかかります。私がよく行うのは、ディレクトリ検索の出力を定期的にデータベーステーブルにダンプし、そのテーブルでルックアップを実行することです。もちろん、データベースダンプの頻度は、ビジネスロジックによって異なります。新しいダンプを実行する前にテーブルを単純に切り捨てる場合もあれば、「ステージング」テーブルにダンプし、アクティブなdirectoyレコードテーブルにのみ「delta」更新を適用する場合もあります。

于 2012-10-18T07:49:53.833 に答える
0
  • 可能であればSSLを使用して、ディレクトリサーバーに接続します。StartTLS拡張操作を使用して非セキュア接続をセキュア接続にプロモートすることも可能です。
  • クライアントが検索の開始を希望するベースDN、検索の範囲(ベース、1レベル、またはサブツリー)、LDAPクライアントが認識している情報を使用するフィルターを含むサーバーにSEARCH要求を送信します。検索結果を目的のユーザーと属性に絞り込みます1.1
  • サーバーは、検索要求パラメーターに一致したエントリーの数と、一致した各エントリーの識別名を含むSEARCH応答で応答します。
  • 安全な接続を介してディレクトリサーバーにBIND要求を送信します。BIND要求には、識別名と識別名の資格情報が含まれています
  • ディレクトリサーバーは資格情報を確認し、資格情報がサーバーデータベースに保存されているものと一致したかどうかを示す整数の結果コードを含むBIND応答を返します。

も参照してください

于 2012-10-18T07:56:18.583 に答える