如何在 System.DirectoryServices 中的调用之间保留连接凭据?

发布于 2024-10-16 15:58:51 字数 300 浏览 6 评论 0原文

我正在尝试连接到不同林中的 Active Directory 域 (W2K8R2 DC)。为此,我将凭据传递到以下 DirectoryEntry 构造函数中:

DirectoryEntry(string path, string username, string password, AuthenticationTypes authenticationType)

这一切都很好。不过,我想要做的是以某种方式保留连接,并在对 AD 的所有调用中重复使用它,这样我就不需要重复传递凭据。这有可能吗?

谢谢!

I am trying to connect to an Active Directory domain (W2K8R2 DC) in a different forest. To that end, I pass the credentials into the following DirectoryEntry constructor:

DirectoryEntry(string path, string username, string password, AuthenticationTypes authenticationType)

This is all good and well. What I would like to do though is retain the connection somehow and reuse it through all my calls to the AD so that I do not need to pass the credentials repeatedly. Is this possible somehow?

Thanks!

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

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

发布评论

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

评论(1

凉栀 2024-10-23 15:58:51

如果您希望在连接级别进行控制,我建议您使用 System.DirectoryServices。协议。您可以重复使用 LDAP 连接来进行不同的 LDAP 查询。然而,编程范式与DirectoryEntry有很大不同

,如果您需要使用DirectoryEntry,则必须将用户名和密码存储在某处,然后将它们传递给所有DirectoryEntry 对象。我要做的就是编写一个方法 GetDirectoryEntry(string dn) 并让该方法使用正确的用户名和密码为我创建 DirectoryEntry 。这看起来不太优雅,但并没有做错什么。如果您希望密码以纯文本形式存储在内存中,请使用 SecureString< /a> 存储密码。

这没有什么问题,因为 DirectoryEntry 正在维护自己的 LDAP 连接池。如果您有多个具有相同用户名和密码的 DirectoryEntry,它将足够智能地共享 LDAP 连接。它基本上与持有单个 LDAP 连接并执行不同的 LDAP 查询相同。它不会针对每个 DirectoryEntry 对象重新向 LDAP 服务器进行身份验证

如果您不喜欢依赖 DirectoryEntry 的黑盒功能,请使用以下建议的解决方法可能会让你感觉好一点。

static DirectoryEntry GetObject(DirectoryEntry root, string dn)
{
    using (DirectorySearcher searcher = new DirectorySearcher(root))
    {
        searcher.Filter = "(distinguishedName=" + dn + ")";
        searcher.SearchScope = SearchScope.Subtree;
        SearchResult result = searcher.FindOne();
        if (result == null) return null;
        return result.GetDirectoryEntry();
    }
}

您只需使用用户名和密码绑定到根对象即可。然后,您可以将根对象保留为静态变量或任何您喜欢的变量。然后,您可以通过将 SearchRoot 设置为根对象来执行 LDAP 查询,从而获得另一个 DirectoryEntry 对象。返回的 DirectoryEntry 仍将使用 root 的用户名和密码。同样,这并不比简单地将用户名和密码传递给 DirectoryEntry 更好。事实上,从性能角度来看,情况更糟,因为我们需要再执行一次 LDAP 查询来获取 DirectoryEntry

If you want the control at the connection level, I recommend you to use System.DirectoryServices.Protocol. You can reuse your LDAP connection to make different LDAP queries. However, the programming paradigm is very different from DirectoryEntry

If you need to use DirectoryEntry, you have to store the username and password somewhere and then pass them to all the DirectoryEntry objects. What I would do is to write a method GetDirectoryEntry(string dn) and have this method create the DirectoryEntry for me with the correct username and password. This doesn't look elegant but it doesn't do anything wrong. If you care password being stored in memory in plain text, use SecureString to store the password.

This is nothing wrong because DirectoryEntry is maintaining its own LDAP connection pool. If you have multiple DirectoryEntry with the same username and password, it will be smart enough to share the LDAP connection. It's basically the same as holding a single LDAP connection and doing different LDAP queries. It's not going to re-authenticate to LDAP server for each of the DirectoryEntry objects

If you don't like to rely on the black box feature from DirectoryEntry, the following suggested workaround may make you feel better.

static DirectoryEntry GetObject(DirectoryEntry root, string dn)
{
    using (DirectorySearcher searcher = new DirectorySearcher(root))
    {
        searcher.Filter = "(distinguishedName=" + dn + ")";
        searcher.SearchScope = SearchScope.Subtree;
        SearchResult result = searcher.FindOne();
        if (result == null) return null;
        return result.GetDirectoryEntry();
    }
}

You just need to bind to a root object with username and password. Then, you can keep the root object as a static variable or whatever you like. Then, you get another DirectoryEntry object by doing a LDAP query with the SearchRoot set to your root object. The returned DirectoryEntry will still use the username and password from root. Again, this is not doing anything better than simply passing in username and password to DirectoryEntry. Indeed, performance-wise, it's worse because we need to do one more LDAP query to get the DirectoryEntry

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