【问题标题】:Authenticate user from specific groups on LDAP server using C#使用 C# 对 LDAP 服务器上特定组的用户进行身份验证
【发布时间】:2018-07-10 12:09:30
【问题描述】:

我在 LDAP 服务器上有一些嵌套组和这些组中的用户。 如何通过仅在组(而不是整个域)中搜索来验证具有给定用户名和密码的用户?绑定会这样做吗?

【问题讨论】:

  • 你有什么样的LDAP服务器?您可以通过提供绑定 OU 来限制搜索范围,但我不确定您使用的是什么 LDAP 服务。
  • 我不知道,我只列出了od域和组,例如domainName.com dc=first, dc = second, ou =third... 而用户和密码都存储在那个ou组,但当然,我看不到他们。我只需要检查他们是否在该组中并且密码是否正确。我可以只使用绑定方法吗?
  • 其实我问的是LDAP服务器的类型。您能否确认它是否是基于 Microsoft 的 Windows Active Directory 服务器?
  • 是的。我可以使用 Novell.Directory.Ldap 或 System.DirectoryServices.Protocols LdapConnection 类进行访问。

标签: c# authentication active-directory ldap nested-groups


【解决方案1】:

正如您在此问题的评论部分所确认的,您所说的 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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多