我什么时候需要域名和域容器来创建 PrincipalContext?
- 作者: 少年未老丶心已荒凉
- 来源: 51数据库
- 2022-10-21
问题描述
我正在开发一个 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
