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.";
            }
        }
    }

是否可以将检查“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 或 Authenticated - 但不在组中

于 2013-10-25T13:33:06.773 回答