我什么时候需要域名和域容器来创建 PrincipalContext?

When do I need a Domain Name and a Domain Container to create a PrincipalContext?(我什么时候需要域名和域容器来创建 PrincipalContext?)
本文介绍了我什么时候需要域名和域容器来创建 PrincipalContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个 C# .NET Framework 库来访问活动目录.

I'm developing a C# .NET Framework library to access active directory.

我必须做的一件事是获取所有 AD 用户,我看到了:

One of the things that I have to do is to get all AD users, and I see that:

PrincipalContext principalContext =
    new PrincipalContext(ContextType.Domain,
                            domainName.Trim(),
                            domainContainer.Trim());

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);

使用此代码返回相同的用户:

Returns the same users with this code:

// define a "query-by-example" principal - here, we search for all users
UserPrincipal qbeUser = new UserPrincipal(principalContext);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach (var found in srch.FindAll())
{
    UserPrincipal user = found as UserPrincipal;
    if (user != null)
    {
        Console.WriteLine(user.SamAccountName);
    }
}

我什么时候需要使用域名和域容器?

When do I need to use a Domain Name and a Domain Container?

推荐答案

使用时

var context = new PrincipalContext(ContextType.Domain);

它将连接到当前上下文的域,通常是运行应用程序的用户登录的域,或者如果当前上下文是未连接到域的本地用户,则会抛出异常.

It will connect to the domain of the current context, usually the domain the user who ran the application is logged into, or will throw an exception if the current context is a local user not connected to a domain.

使用时

var context = new PrincipalContext(ContextType.Domain, domainName, domainContainer);

域属性允许您连接到当前上下文以外的域,假设当前上下文具有权限或您提供有效凭据.因此,例如,在林中有多个域或域信任的环境中,您可以指定另一个域来运行查询,而不是用户所属的域.

The domain property allows you to connect to a domain other than the one of the current context, assuming the current context has permissions or you supply valid credentials. So for example in an environment where there is multiple domains in a forest or domain trusts in place, you can specify another domain to run queries against instead of the one the user is a member of.

容器属性将使用该 DomainContext 的所有查询限制到指定的容器.

The container properties limits all queries using that DomainContext to the specified container.

这篇关于我什么时候需要域名和域容器来创建 PrincipalContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to determine the type (AD User vs. AD Group) of an account?(如何确定帐户的类型(AD 用户与 AD 组)?)
How to resolve quot;The server does not support the control. The control is critical.quot; Active Directory error(如何解决“服务器不支持控件.控制至关重要.活动目录错误)
How to authenticate users with a customer#39;s (remote) active directory server(如何使用客户的(远程)活动目录服务器对用户进行身份验证)
How to know if my DirectoryEntry is really connected to my LDAP directory?(如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?)
Add member to AD group from a trusted domain(将成员从受信任的域添加到 AD 组)
How to retrieve Users in a Group, including primary group users(如何检索组中的用户,包括主要组用户)