【问题标题】:Active Directory: How to determine whether account is service account?Active Directory:如何确定帐户是否为服务帐户?
【发布时间】:2018-11-02 23:43:09
【问题描述】:

问题:是否可以使用 C# LDAP 确定帐户是否是 Active Directory 中的服务帐户?如果是,如何?

上下文:我有一个程序正在检索架构类类型 USER、GROUP、COMPUTER、FOREIGN SECURITY PRINCIPAL 和 CONTACT 的所有对象。目前,服务帐户是通过字符串解析“服务帐户”的规范名称来识别的。我不喜欢这个解决方案,因为字符串解析依赖于层次结构中的文件夹位置,字面意思是“服务帐户”。似乎可以创建一个服务帐户,然后将其放置在不包含字符串“服务帐户”的文件夹路径中。不幸的是,我无法对此进行测试,因为我不是 AD 管理员。

我在网上浏览过没有任何运气,所以我不确定它是否可能。

更新:

根据Microsoft,服务帐户似乎包含在 objectClass msDS-ManagedServiceAccount 中。但是,当我将 DirectoryEntry 筛选器设置为 msDS-ManagedServiceAccount 时,不会返回任何结果。

directoryEntry = new DirectoryEntry(strActiveDirectoryHost, null, null, AuthenticationTypes.Secure);
string strDsFilter = "(objectClass=msDS-ManagedServiceAccount)";

DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry)
{
    Filter = strDsFilter,
    SearchScope = SearchScope.Subtree,
    PageSize = intActiveDirectoryPageSize,
};

return searchResultCollection = directorySearcher.FindAll();

【问题讨论】:

  • 服务帐户是指托管服务帐户,还是其他?如果您可以发布一些代码,那将有所帮助。
  • @RyanS 我已经更新了我的问题。

标签: c# active-directory ldap


【解决方案1】:

我已经测试了您的代码,它确实在我的环境中返回了结果。需要注意的几点:

  • 确保strActiveDirectoryHost 的格式正确。格式应为LDAP://DC=contoso,DC=com
  • 检查您是否从根目录(或足够高以找到您要查找的帐户)进行搜索。 MSA 在域 NC 下的Managed Service Accounts 容器下(即LDAP://CN=Managed Service Accounts,DC=contoso,DC=com
  • 在我的测试中,我只用路径调用new DirectoryEntry()。不确定传递 AuthenticationTypes.Secure 是否会给您带来问题
  • 您拥有的 objectClass 是正确的。

【讨论】:

  • 好的,所以在 ADUC 中我看到 Managed Service Accounts 文件夹是空的,这可能解释了为什么我收到 0 个结果。但是,还有另一个名为“Service Accts”的文件夹。您知道托管服务帐户和服务帐户有什么区别吗?
  • 你能找到这些“服务帐户”对象之一并转储它的 objectClass 列表吗?
  • 从0到3,分别是“top”、“person”、“orgranizationalPerson”、“user”。
  • 那么你的“服务帐号”只是一个用户。托管服务帐户是一种特殊类型的帐户,其密码由域管理。听起来只有普通用户帐户被“指定”为服务帐户。
  • 这真的取决于您要使用该帐户的目的。 MSA 和普通用户帐户之间最重要的区别是密码由 AD 管理,类似于计算机帐户。使用普通用户帐户作为服务帐户没有任何问题。但是在其他用户帐户中找到它们需要以某种方式指定它们,例如名称中包含“serviceAccount”。否则,它们看起来就像普通用户帐户。
【解决方案2】:

所以我正在努力获取 MSA 并创建它们。我能够使用 System.DirectoryServices.AccountManagement 命名空间获得 MSA,仍在努力创建它(不确定这是否真的可能) 但是要查找属于 MSA 的帐户,您可以使用以下代码

PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
GroupPrincipal currentGroup = GroupPrincipal.FindByIdentity(oPrincipalContext, "YourGroupName");
        foreach (Principal a_principal in currentGroup.GetMembers())
        {
            if (a_principal.StructuralObjectClass == "msDS-ManagedServiceAccount")
            {
                Console.Write(a_principal.SamAccountName); //To get the name
                ComputerPrincipal oComputerPrincipal = ComputerPrincipal.FindByIdentity(oPrincipalContext, a_principal.Name); //creating a computerprincipal to get more details about the MSA

            }
        }

您可以使用上述逻辑,为用户帐户创建一个 Principal 并获取该帐户的结构对象类,以确定它是否为 MSA。 像这样的:

 UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
      if (oUserPrincipal.StructuralObjectClass == "msDS-ManagedServiceAccount")
                {
                    Console.Write(oUserPrincipal.SamAccountName); //To get the samaccountname
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多