简体   繁体   English

如何获得其他域中用户的UserPrincipal?

[英]How can I get a UserPrincipal for a user in a different domain?

The following code works great for users in my domain (eg, "TESTER" instead of "DEVELOPER"), but I can't figure out how to search higher than the current domain. 以下代码非常适合我所在域中的用户(例如,“ TESTER”而不是“ DEVELOPER”),但我不知道如何搜索比当前域更高的域名。 I tried variation combinations of searches with the PrincipalSearcher class, but I'm not sure how to pass in a search by email address or username to look for other domains within my organization even though they're all in a single forest. 我尝试使用PrincipalSearcher类进行搜索的变体组合,但是我不确定如何通过电子邮件地址或用户名传递搜索以查找组织中的其他域,即使它们都位于单个林中也是如此。

var name = "DEVELOPER\\JULIANI99";
var p = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), name);

FindByIdentity doesn't work well for searching a forest. FindByIdentity不适用于搜索森林。

I tried something like this: 我尝试过这样的事情:

var d = new PrincipalContext(ContextType.Domain, "domain.com:3268", "DC=com");
var p = UserPrincipal.FindByIdentity(d, IdentityType.SamAccountName, username);

But I keep getting an error saying a referral was returned. 但是我一直收到错误消息,说转诊已返回。 It might be different for you. 您可能会有所不同。 The "3268" port tells it to use the global catalog (forest-wide search). “ 3268”端口告诉它使用全局目录(林范围内的搜索)。 The root (which I have as "DC=com") has to be the common across all the domains in your forest. 根目录(我的目录为“ DC = com”)必须是您林中所有域的公共目录。 So if all your domains are sub-domains of "domain.com", then you could put "DC=domain,DC=com". 因此,如果您所有的域都是“ domain.com”的子域,则可以输入“ DC = domain,DC = com”。 But if you have "domain.com" and "otherdomain.com" part of the same AD forest, then that wouldn't work. 但是,如果您在同一个AD林中包含“ domain.com”和“ otherdomain.com”部分,则该方法将无效。

FindByIdentity also won't work for searching by email address, so you may just be better off using PrincipalSearcher . FindByIdentity也无法通过电子邮件地址进行搜索,因此使用PrincipalSearcher可能会更好。

If you get the same referral error I got, you can tell it to follow the referral: 如果遇到与我相同的引荐错误,则可以告诉它遵循引荐:

PrincipalSearcher srch = new PrincipalSearcher(User);
((DirectorySearcher) srch.GetUnderlyingSearcher()).ReferralChasing = ReferralChasingOption.All;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM