使用LdapConnection枚举AD

发布于 2025-01-01 16:59:23 字数 1478 浏览 0 评论 0原文

是否可以使用 System.DirectoryServices.Protocols 中的 LdapConnection 来查询 Active Directory?

我在实例化 PrimaryContext 时遇到问题。这是我的代码,以防任何人都可以发现问题:

    private LdapConnection getLdapConnection(string username, string password, string ldapServer, bool secured)
    {
        int port = secured ? 636 : 389;

        LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(ldapServer, port, false, false));

        if (secured)
        {
            connection.SessionOptions.ProtocolVersion = 3;
            connection.SessionOptions.SecureSocketLayer = true;
        }


        connection.Credential = new NetworkCredential(username, password);
        connection.AuthType = AuthType.Basic;
        connection.SessionOptions.VerifyServerCertificate += (conn, cert) => { return true; };
        connection.Bind();

        return connection;
    }

当尝试实例化我正在使用的主体上下文时,

        PrincipalContext context = new PrincipalContext(
            ContextType.Domain,
            ldapServer,
            null,
            useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
            username,
            password);

我传递相同的值,为了完整起见,用户名的格式为 domain\user 并且 ldapServer 的格式为 server.domain.com,其中 ldapServer 在创建主体上下文时附加了 :636。

我连接到的服务器存在证书问题,我猜想这可能会阻止这种情况,因为 LdapConnection 设置为返回 true 进行验证。这不是问题,因为它是可信的。我无权访问服务器,因此无法更改此设置。

Is it possible to use an LdapConnection from System.DirectoryServices.Protocols to query Active Directory?

I'm having issues instantiating a PrincipalContext. Here's my code, in case anyone can spot the issue:

    private LdapConnection getLdapConnection(string username, string password, string ldapServer, bool secured)
    {
        int port = secured ? 636 : 389;

        LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(ldapServer, port, false, false));

        if (secured)
        {
            connection.SessionOptions.ProtocolVersion = 3;
            connection.SessionOptions.SecureSocketLayer = true;
        }


        connection.Credential = new NetworkCredential(username, password);
        connection.AuthType = AuthType.Basic;
        connection.SessionOptions.VerifyServerCertificate += (conn, cert) => { return true; };
        connection.Bind();

        return connection;
    }

When trying to instantiate the Principal Context I am using

        PrincipalContext context = new PrincipalContext(
            ContextType.Domain,
            ldapServer,
            null,
            useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
            username,
            password);

I am passing the same values in, for the sake of completeness username is in the format of domain\user and the ldapServer is in the format of server.domain.com with ldapServer having :636 appended when creating the Principal Context.

The server I am connecting to has certificate issues which I guess could be preventing this as the LdapConnection is set to return true for verification. This isn't an issue as it's trusted. I don't have access to the server and as such cannot change this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2025-01-08 16:59:23

据我了解,当您定位域时,容器参数不能为 null。你能试试这个构造函数吗:

PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
                                                           "server.domain.com :389",
                                                           "dc=domain,dc=com",
                                                           username,
                                                           password);

然后是这个:

PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
                                                           "server.domain.com :636",
                                                           "dc=domain,dc=com",
                                                           useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
                                                           username,
                                                           password);

As far as I understant the container parameter can't be null when you target a domain. Can you just try this constructor :

PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
                                                           "server.domain.com :389",
                                                           "dc=domain,dc=com",
                                                           username,
                                                           password);

And then this one :

PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
                                                           "server.domain.com :636",
                                                           "dc=domain,dc=com",
                                                           useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
                                                           username,
                                                           password);
流心雨 2025-01-08 16:59:23

我怀疑 LDAP 代码的部分问题是您使用的是 AuthType.Basic。请尝试谈判

I suspect part of your problem with the LDAP code is that you're using AuthType.Basic. Try Negotiate instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文