Xi *_*iao 1 c# active-directory
我使用下面的代码片段System.DirectoryServices.AccountManagement
在 ActiveDirectory 中搜索用户。
在user.Name
成功返回,但我怎么能检索其他属性从AD用户,喜欢msExchRecipientTypeDetails
,因为它不是在VisualStudio中2015年的情报显示?
using (PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, DOMAIN, USERNAME, PASSWORD))
{
UserPrincipal userPrincipal = new UserPrincipal(adPrincipalContext);
userPrincipal.SamAccountName = "user-ID";
PrincipalSearcher search = new PrincipalSearcher(userPrincipal);
foreach (var user in search.FindAll())
{
Console.WriteLine("hei " + user.Name);
// how to retrive other properties from AD like msExchRecipientTypeDetails??
}
}
Run Code Online (Sandbox Code Playgroud)
您需要将 DirectoryEntry 用于任何此类自定义属性。如果您还没有,请在您的项目中添加对“System.DirectoryServices”的引用。由于您已经有一个 Principal 对象,您可以这样做来获取 DirectoryEntry:
var de = user.GetUnderlyingObject();
Run Code Online (Sandbox Code Playgroud)
并且因为 msExchRecipientTypeDetails 是一个奇怪的 AD 大整数,您必须跳过箍才能获得真正的值。这是另一个问题的解决方案来获取值:
var adsLargeInteger = de.Properties["msExchRecipientTypeDetails"].Value;
var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
if (lowPart < 0) highPart++;
var recipientType = highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
Run Code Online (Sandbox Code Playgroud)
更新:三年后,我不得不再次查找并偶然发现了我自己的答案。但事实证明,有时我不应该得到负值。我在这里找到了答案:
解决方法是每当 LowPart 方法返回的值为负数时,将 HighPart 方法返回的值增加一。
因此,我添加了一点: if (lowPart < 0) highPart++;
归档时间: |
|
查看次数: |
771 次 |
最近记录: |