c# - 为 PrincipalContext 指定容器后无法找到用户

标签 c# active-directory userprincipal

我正在尝试通过 Active Directory 中的用户名查找用户。

这个有效:

const string Domain = "SLO1.Foo.Bar.biz";
const string Username = "sanderso";

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  Username);

这不是:

const string Domain = "SLO1.Foo.Bar.biz";
const string Container = "CN=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";
const string Username = "sanderso";

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, Username);

我收到错误信息:

There is no such object on the server.

这是我的 ActiveDirectory 设置的屏幕截图:

enter image description here

我也尝试过使用以下容器:

const string Container = "OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";

这同样不成功。

如何在访问“用户”容器时指定我的容器?在引入具有更复杂要求的查找之前,我试图将此作为初始的简单设置。因此,我宁愿不接受简单的解决方案,因为我相信无论如何我都必须解决这个问题。

最佳答案

我想通了:)

首先,我使用以下软件来确保生成正确的容器字符串:

http://www.ldapbrowser.com/download.htm

这证实了我的字符串几乎是正确的,除了缺少一个端口,但它只是需要一些大惊小怪。

正确的用法是:

const string Domain = "SLO1.Foo.Bar.biz:389";
const string Container = @"DC=Foo,DC=Bar,DC=biz";
const string Username = @"sanderso";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  username);

关于c# - 为 PrincipalContext 指定容器后无法找到用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205737/

相关文章:

c# - 扩展用户主体; FindByIdentity() 失败

c# - ASP.NET MVC Windows Authentiaction 和 DirectoryServices - 获取当前用户的邮件地址引发 InvalidCastException

c# - 无法使用 C# TCP 服务器和 C++ TCP 客户端接收消息

javascript - 日期选择器不工作(ASP.NET MVC)

c# - 依赖注入(inject),但不注入(inject) Controller

c# - TimeSpan 的 Range 和 DisplayFormat 属性

python - 如何使用 django-python3-ldap 从 Active Directory 组在 Django 模型中创建组和权限?

java - 带有 Windows AD 的 Tomcat 6 领域配置

c# - 在 web.config 上设置 AD 角色名称

c# - 有没有办法通过 UserPrincipal 类设置新用户的域后缀?