【问题标题】:Should GroupPrincipal supply ALL AD fileds?GroupPrincipal 是否应该提供所有 AD 字段?
【发布时间】:2020-02-19 06:00:16
【问题描述】:

VisualStudio 2019、C#、.NetCore 2.2

发生了一些奇怪的事情。我有以下代码。它工作,但是如果我在var thing = member; 行上设置一个断点,我可以看到member 有很多object.member 值。诸如:EmailAddress 和其他 AD 字段。但是,在编辑器中,如果我尝试使用member.EmailAddress,则会收到无法解析EmailAddress 的消息。

我很确定PrincipalContext 的用户/密码对是一个“特权”帐户,应该有权访问所有 AD 字段,如果这很重要的话。

    using (var ctx = new PrincipalContext(ContextType.Domain, 
        "ad.myCompany.com",  userName:"ad-svc-account", "...")) {
        var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName,
            "CN=My Group Staff,OU=Groups - DLs,OU=My,OU=Org,DC=ad,DC=myCompany,DC=com");

        if (grp == null) {
            return StatusCode(StatusCodes.Status500InternalServerError, "Group list is empty");
        }

        foreach (var member in grp.Members) {
            var thing = member;
        }
    }

我错过了什么?

【问题讨论】:

    标签: c# .net-core-2.2


    【解决方案1】:

    当您使用 var member 遍历组的成员时,您正在迭代类型为 Principal[1] 的成员。 Principal 类型本身没有您通常在 GroupPrincipal[2]UserPrincipal[3] 上看到的详细信息。

    由于组的成员可能是 UserPrincipal 或 GroupPrincipal 或其他类型,因此您需要首先确定您正在使用的 Principal 的类型,将其转换为正确的类型,然后然后查找所有属性。

    foreach(var member in grp.Members) 
    {
      if (member is UserPrincipal)
      {
          var memberP = member as UserPrincipal;
          memberP.<will have all the properties of UserPrincipal>;
      }
      else if (member is GroupPrincipal)
      {
          var memberG = member as GroupPrincipal;
          memberG.<will have all properties of GroupPrincipal>;
      }
      else
      {
        //handle other types (such as ComputerPrincipal) or all others.
      }
    }
    

    [1]Principal

    初始化 Principal 类的新实例。此构造函数由派生类构造函数调用以初始化基类,并不打算直接从您的代码中调用

    [2]GroupPrincipal

    封装组帐户。组帐户可以是为管理目的而创建的主体对象或帐户的任意集合

    [3]UserPrincipal

    封装作为用户帐户的主体

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2016-05-20
      • 1970-01-01
      • 2022-08-14
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多