【问题标题】:DirectoryEntry and Domain ControllerDirectoryEntry 和域控制器
【发布时间】:2017-01-16 06:34:06
【问题描述】:

我们在 Powershell 中有一个脚本,用于操作 Active Directory。我现在用 C# 编写了它。我的同事说他们必须在 PS 中指定域控制器,否则可能会发生使用 DC A 读取并在 DC B 上写入的情况,这可能会导致问题。

如果我使用 DirectorySearcher 查找条目并对其进行操作,我真的必须指定域控制器吗?或者根据定义,相同的域控制器是否是用于查找对象(DirectorySearcher)并保存它(CommitChanges)的用户?

我不这么认为,因为我只能在搜索部分(DirectorySearcher 的 DirectoryEntry 对象)中指定它,但不能在将它写回 AD 时指定它(CommitChanges)。所以我猜想写的 DC 和读的 DC 一样。

下面我有一个示例,我搜索特定条目并更改属性。

string filter = "(proxyaddresses=SMTP:johndoe@abc.com)";
string searchOU = "ou=Users,dc=abc,dc=com";

DirectoryEntry entry = new DirectoryEntry("LDAP://" + searchOU);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = filter;
SearchResult result = search.FindOne();
search.Dispose();
entry.Close();

DirectoryEntry toContact = result.GetDirectoryEntry();    
toContact.Properties["showInAddressBook"].Value = addressbook; 
toContact.CommitChanges();
toContect.Close();

【问题讨论】:

    标签: c# active-directory directoryentry domaincontroller directorysearcher


    【解决方案1】:

    我可以推荐使用 System.DirectoryServices.AccountManagement 命名空间中可用的对象吗?它是 .NET 的最新成员,可以更优雅地处理 AD 工作。它最近为我节省了很多心痛,这是旧的 DirectorySearcher 做事方式的部分原因。让框架承受压力!网上有很多非常有用的文章,比如here

    作为如何使用 PrincipalContext 搜索 Active Directory 的示例,试试这个:

    var adContext = new PrincipalContext(ContextType.Domain);
    var queryTemplateUser = new UserPrincipal(adContext);
    queryTemplateUser.SamAccountName = "HenryCrunn";
    var ldapSearcher = new PrincipalSearcher(queryTemplateUser);
    var searchResults = ldapSearcher.FindAll();
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 是的,你当然是对的。抱歉,我会用一些有用的代码更新答案。
    • @DrMistry:微软的一个人让我使用 DirectorySearcher,因为它更灵活。问题是该链接也没有回答有关域控制器的问题。如果我更新找到的对象,'searchResults' 中的对象是否会“记住”他们使用哪个 DC 来查找对象并使用相同的对象?顺便说一句:使用 FindAll() 时要小心,你必须处理找到的对象,否则你有内存泄漏。
    • 您可以从 PrincipalContext.ConnectedServer 中获取连接服务器的名称,我认为这将是处理所有请求的 DC。
    • 如果我猜对了,你也会认为它是同一个 DC。有没有办法检查这一点,或者这是否记录在案?
    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多