使用 C# 对来自 LDAP 服务器上特定组的用户进行身份验证

Authenticate user from specific groups on LDAP server using C#

我在 LDAP 服务器上有几个嵌套组,这些组中有用户。 如何通过仅在组(而不是整个域)中搜索来使用给定的用户名和密码对用户进行身份验证? bind 这样做吗?

正如您在此问题的评论部分所确认的那样,您所说的 LDAP 服务器是一个 Active Directory 服务器。所以,我的答案是基于this famous answer about how to validate a username and password against Active Directory,只是我根据你的要求做了修改,限制了搜索范围。

如果您使用 .NET 3.5 或更高版本,您可以使用 System.DirectoryServices.AccountManagement 命名空间的 PrincipalContext Constructor (ContextType, String, String) 并轻松验证您的凭据:

// create a "principal context"
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR.DOMAIN",
             "OU=Where,OU=You,OU=Wanna,OU=Search,DC=YOUR,DC=DOMAIN"))
   // change your container to a base OU where all your users are located.
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}