【问题标题】:How to validate LDAP path against Active Directory如何针对 Active Directory 验证 LDAP 路径
【发布时间】:2016-08-08 06:58:58
【问题描述】:

如何验证LDAP 路径?我有三个textboxes,允许用户输入 LDAP 路径、用户名和密码。我能够验证用户名和密码,但在验证 LDAP 路径时,它最初可以工作,但一段时间后,它也允许无效路径。

有效的 LDAP 路径:

192.168.12.12:565

LDAP 路径无效:

gfg192.168.12.12:565fgfgf

并且用户能够获取使用无效路径的用户列表。

我试过LdapConnection,使用Directory Entry和使用PrincipalContext

LdapConnection connection = new LdapConnection(txtLDAPPath.Text.Trim());
NetworkCredential credential = new NetworkCredential(txtADUserName.Text.Trim(), password);
connection.Credential = credential;
connection.Bind();

using (DirectoryEntry entry = new DirectoryEntry())
{
    entry.Username = txtADUserName.Text.Trim();
    entry.Password = password;
    entry.Path = txtLDAPPath.Text;
    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(&(objectClass=user)(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))";
    object obj = entry.NativeObject;
    SearchResult resultCol = search.FindOne();
}


PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"Domain");
bool Validate= ctx.ValidateCredentials(txtADUserName.Text, password);

【问题讨论】:

  • 到目前为止你尝试过什么?你真的希望社区在不分享你的代码的情况下做什么?请参考How do I ask a good question?
  • 我已经发布了我的代码@uteist 我尝试了提到的各种方法,但没有一个验证 LDAP 路径。
  • 快速浏览一下,我可以看出您实际上根本没有使用LdapConnection。您可能会发现 this link 对您的 AD 应用很有帮助。
  • 我已经一一尝试了所有这些方法,但似乎没有人在验证 LDAP 路径。@uteist
  • 我允许用户在文本框中输入 LDAP 路径并进行验证。@Burzum

标签: c# asp.net authentication active-directory


【解决方案1】:

我发现的唯一方法是使用 System.DirectoryServices.DirectorySearcher.FindOne() 方法

        var paths = new[]
        {
            new { Path = "LDAP://192.168.1.1:389/OU=Users,OU=Administration,DC=ac-qa,DC=aaaa,DC=se", Filter = "(&(objectClass=user))" }, //OK! returns the first entry
            new { Path = "LDAP://192.168.1.1:389/OU=Users,OU=Administration,DC=ac-qa,DC=aaaa,DC=se", Filter = "this is wrong" }, //ERROR! the exeption message: "The this is wrong search filter is invalid."
            new { Path = "the wrong path", Filter = "(&(objectClass=user))" },      //ERROR! the exeption message: "Unspecified error"
            new { Path = "LDAP://192.168.1.1:389/OU=Test_OrgUnit,DC=ac-qa,DC=aaaa,DC=se", Filter = "(&(objectClass=user))" }, //OK! This is a valid path without any entry inside. result = null
        };

        foreach (var item in paths)
        {
            DirectoryEntry entry = new DirectoryEntry(item.Path, Login, Password);
            DirectorySearcher search = new DirectorySearcher(entry, item.Filter);

            try
            {
                SearchResult result = search.FindOne();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多