根据 AD 组检查用户登录

本文关键字:用户 登录 检查 AD 根据 | 更新日期: 2023-09-27 18:36:43

我的 C# Web 应用程序中有一个使用 DirectoryEntry 方法的 LDAP 登录设置。 下面的代码是我到目前为止所拥有的。 这将允许任何拥有AD帐户的人登录。 我需要将其限制为名为"普通用户"的组中的人。

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

是否可以将检查"普通用户"组的用户帐户的函数添加到这些函数之一中?

根据 AD 组检查用户登录

您希望捕获 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

如果在 .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;
                }

您可以更改以返回两种类型的错误:"未经过身份验证"或"已验证 - 但不在组中"