【问题标题】:How can I specify alternate credentials in code?如何在代码中指定备用凭据?
【发布时间】:2011-03-29 10:15:47
【问题描述】:

我想运行这个功能,或者至少是使用不同凭据从 AD 中删除机器帐户的位:

 public static void DeleteMachineAccount(String MachineName)
        {
            String MachineLdapPath = LdapPath(MachineName);
            String OuLdapPath = MachineLdapPath.Replace("CN=" + MachineName + ",", "");

            Console.WriteLine(MachineLdapPath);
            Console.WriteLine(OuLdapPath);

            if (DirectoryEntry.Exists(MachineLdapPath))
            {
                try
                {
                    DirectoryEntry MachineOu = new DirectoryEntry(OuLdapPath);
                    DirectoryEntry MachineToDelete = new DirectoryEntry(MachineLdapPath);
                    MachineOu.Children.Remove(MachineToDelete);
                    MachineToDelete.CommitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message.ToString());
                }
            }

        }

(LdapPath 函数只返回指定机器名称的 LDAP 路径。)

我如何/在哪里指定一些不同的凭据以允许它运行?目前我被拒绝访问,因为我使用的帐户无权执行此操作。

谢谢,

【问题讨论】:

    标签: c# permissions active-directory


    【解决方案1】:

    您可以使用提供身份验证的 DirectoryEntry 类的重载。这将导致您的 LDAP 查询以该特定用户的权限从 DirectoryServices 运行。请注意,为了做到这一点,您需要传递凭据(需要由用户存储或输入),因此在处理它们时要小心。以纯文本形式存储它们可能会导致系统安全问题。

    New DirectoryEntry(ldapRoot, _activeDirectoryUsername, _activeDirectoryPassword);
    

    【讨论】:

      【解决方案2】:

      您需要使用模拟。最简单的方法是实际“借用”调用此方法的人的许可。例如,如果这是从命名管道或 WCF 调用中调用的,则有内置方法可以模拟调用者并代表他们执行此操作。

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 2019-07-06
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多