【问题标题】:C# access msExchRecipientTypeDetails property by System.DirectoryServices.AccountManagementC# 通过 System.DirectoryServices.AccountManagement 访问 msExchRecipientTypeDetails 属性
【发布时间】:2016-02-18 07:22:22
【问题描述】:

我使用下面来自System.DirectoryServices.AccountManagement 的代码 sn-p 在 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??          
    }
}

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    您需要对任何类似的自定义属性使用 DirectoryEntry。如果您还没有,请在您的项目中添加对“System.DirectoryServices”的引用。由于您已经有一个 Principal 对象,您可以这样做来获取 DirectoryEntry:

    var de = user.GetUnderlyingObject();
    

    而且由于 msExchRecipientTypeDetails 是一个奇怪的 AD 大整数,因此您必须跳过铁环才能获得真正的值。这是来自another question 的获取值的解决方案:

    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;
    

    更新:三年后,我不得不再次查找此内容并偶然发现了自己的答案。但事实证明,有时我不应该得到负值。我找到了答案here

    解决方法是每当 LowPart 方法返回的值为负时,将 HighPart 方法返回的值加一。

    因此,我添加了那个位:if (lowPart &lt; 0) highPart++;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多