我什么时候需要域名和域容器来创建 PrincipalContext? [英] When do I need a Domain Name and a Domain Container to create a PrincipalContext?

查看:17
本文介绍了我什么时候需要域名和域容器来创建 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆