c# - 事件目录服务 : PrincipalContext -- What is the DN of a "container" object?

标签 c# .net-3.5 active-directory directoryservices

我目前正在尝试使用 PrincipalContext 类通过 Active Directory 服务进行身份验证。我想让我的应用程序使用密封和 SSL 上下文对域进行身份验证。为此,我必须使用 the following constructor of PrincipalContext (link to MSDN page) :

public PrincipalContext(
    ContextType contextType,
    string name,
    string container,
    ContextOptions options
)

具体来说,我是这样使用构造函数的:

PrincipalContext domainContext = new PrincipalContext(
    ContextType.Domain, 
    domain, 
    container, 
    ContextOptions.Sealing | ContextOptions.SecureSocketLayer);

MSDN 关于“容器”的说法:

The container on the store to use as the root of the context. All queries are performed under this root, and all inserts are performed into this container. For Domain and ApplicationDirectory context types, this parameter is the distinguished name (DN) of a container object.

容器对象的 DN 是什么?我如何找出我的容器对象是什么?我可以为此查询 Active Directory(或 LDAP)服务器吗?

最佳答案

好吧,我设法弄清楚了问题:

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain,domain);

domainContext.ValidateCredentials(userName, password, 
    ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);

通过在 ValidateCredentials 方法中(而不是在构造函数中)指定 ContextOptions,这让我不必为容器对象指定 DN。

更新:

虽然我应该澄清的是,经过进一步的实验,我发现从这个 PrincipalContext 对象派生的任何查询都是未加密的。

显然,当在 ValidateCredentials 中设置 ContextOptions 时,这些选项仅用于特定的 ValidateCredentials 调用。但这就是它变得奇怪的地方......

因此,我希望对 AD 服务器的查询也进行加密。示例查询:

UserPrincipal p = UserPrincipal.FindByIdentity(
    domainContext, IdentityType.SamAccountName, userName);
var groups = p.GetGroups();
foreach (GroupPrincipal g in groups) { /* do something */ }

上面的代码获取用户所属的所有组的列表,但它是明文(未加密)的。因此,经过多次摆弄后,我发现 DN 根本不需要设置。

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain,domain,
    null,ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);

我发现我可以将容器对象 (DN) 设置为空。这很好用。将它设置为空字符串 ("") 会导致某种未知类型的异常,所以不要以为可以给它一个空字符串。

这是奇怪的部分。您可能认为在 PrincipalContext 中设置 SecureSocketLayer 选项意味着您在使用 VerifyCredentials 时不必显式设置它。但我发现,如果我没有在 VerifyCredentials 部分设置它,身份验证将失败,但查询(如在组的示例中)仍然是加密的。

也许我还没有完全理解 AD 身份验证和查询,但这对我来说似乎是一种奇怪的行为。

关于c# - 事件目录服务 : PrincipalContext -- What is the DN of a "container" object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2538064/

相关文章:

c# - 有没有办法编写独立于系统的MVC应用程序?

c# - 检查字符串是否包含 10 个字符之一

c# - 将重复小数格式化为分数

c# - 获取硬件信息

c# - 5 个参数的函数

c# - 单击按钮获取行的 DataKeyNames

c# - 组合多个 SubSonic.Where 过滤器

c++ - 确定跨域 Active Directory 组成员身份

c# - 使用 C# 向 Active Directory 注册更改通知

active-directory - 如何查看事件目录中已删除的对象