2

DirectoryEntry メソッドを使用する C# Web アプリで LDAP ログインをセットアップしています。以下のコードは、私がこれまでに持っているものです。これにより、AD アカウントを持つすべてのユーザーがログインできるようになります。「commonusers」という名前のグループの人に限定する必要があります。

    public Boolean ValidateUser(string userName, string password)
    {
        string path = "LDAP://domain.company.org";
        DirectoryEntry dirEntry = new DirectoryEntry(path, userName, password, AuthenticationTypes.Secure);
        try
        {
            DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);
            dirSearcher.FindOne();
            return true;
            // If it returns the data then the User is validated otherwise it will automatically shift to catch block and return false
        }
        catch
        {
            return false;
        }

ログイン ボタンは、次のコードを使用します。

    protected void Button1_Click(object sender, EventArgs e)
    {
        {
            Boolean boolresult = ValidateUser(TextBox_username.Text, TextBox_password.Text);
            if (boolresult)
            {
                Label_loginstatus.Text = "Redirecting";

                Response.Redirect("medsearch.aspx");
            }
            else
            {
                Label_loginstatus.Text = "Invalid username/password! Please try again.";
            }
        }
    }

これらの機能の 1 つに、「commonusers」グループのユーザー アカウントをチェックする機能を追加することは可能ですか?

4

2 に答える 2

0

FindOne() 呼び出しの結果をキャプチャして、気になるプロパティ値を探したいとします。

DirectoryEntry dirEntry = new DirectoryEntry(path, userName, password, AuthenticationTypes.Secure);
object obj = de.NativeObject;  // This will force the authentication

// Search for the user's directory entry
DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);
dirSearcher.Filter = "(SAMAccountName=" + userName + ")";
dirSearcher.PropertiesToLoad.Add("member");
SearchResult searchResult = dirSearcher.FindOne();

if (searchResult != null)
{
    if (searchResult.Properties["member"] != null && searchResult.Properties["member"].Contains("commonusers"))
    {
        return true;
    }
}

return false;

Active Directory からデータを取得する方法の詳細については、www.selfadsi.orgにアクセスすることをお勧めします。

于 2013-10-24T22:45:57.337 に答える
-1

.NET 4 の場合、try/catch なしで実行できるこのメソッドを使用できます。

     private bool ValidateAgainstADAndGroup(string username, string password, string groupname)
                {
                    var ok = false;
                    using (var pc = new PrincipalContext(ContextType.Domain, "mydomain.lan"))
                    {
                        if (pc.ValidateCredentials(username, password))
                        {
                            //User is alright
                            using (var searcher = new PrincipalSearcher(new UserPrincipal(pc)))
                            {
                                searcher.QueryFilter.SamAccountName = username;
                                Principal u = searcher.FindOne();
                                foreach (Principal p in u.GetGroups())
                                {
                                    if (p.Name == groupname)
                                    {
                                        //User is in group
                                        ok= true;
                                    }
                                }
                            }
                        }
                    }

                    return ok;
                }

NotAuthenticated OR Authenticated の 2 種類のエラーを返すように変更できますが、グループ内ではありません

于 2013-10-25T13:33:06.773 に答える