我正在尝试通过 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 设置的屏幕截图:
我也尝试过使用以下容器:
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/