C# 主体不包含';电子邮件地址';
我正在尝试通过Windows服务应用程序从Windows 2016服务上的OU检索用户(以将帐户同步到其他服务) 我有下面的代码来搜索OU,然后将结果添加到列表中C# 主体不包含';电子邮件地址';,c#,windows-services,directoryservices,C#,Windows Services,Directoryservices,我正在尝试通过Windows服务应用程序从Windows 2016服务上的OU检索用户(以将帐户同步到其他服务) 我有下面的代码来搜索OU,然后将结果添加到列表中 List<string> UserList = new List<string>(); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, OU); UserPrincipal userqbe = new Us
List<string> UserList = new List<string>();
PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, OU);
UserPrincipal userqbe = new UserPrincipal(context);
PrincipalSearcher searcher = new PrincipalSearcher(userqbe);
foreach (var found in searcher.FindAll())
{
UserList.Add(found.EmailAddress);
}
about The user principle类确实显示了一个
EmailAddress
属性。其他属性,如DisplayName
可以正常工作
我在这里遗漏了什么?检查这里的
FindAll
文档:
返回
PrincipalSearchResult<Principal>
请注意,如果您不确定主体类型,这是危险的。为了避免任何运行时错误,您可以使用Is操作符foreach(在searcher.FindAll().OfType()中找到的变量)?拼写问题。“校长”!=“原则”。
EmailAddress
属性有两个Ds,而您的问题标题EmailAddress
缺少一个Ds。因此,您的问题可能是“打字错误”,或者您没有显示实际的错误消息。@stuartd解决了问题,请将其添加为答案,我将接受它。
PrincipalSearchResult<Principal>
UserList.Add(((UserPrincipal)found).EmailAddress);