【发布时间】:2022-01-22 23:53:17
【问题描述】:
我想使用 C# 从 Active Directory 获取增量更改。为此,我正在尝试构建以下文章中提到的解决方案。
https://docs.microsoft.com/en-us/windows/win32/ad/polling-for-changes-using-usnchanged
但是,我面临以下问题:
- 没有可供用户使用的 uSNChanged 属性(尽管它可用于 OU,即组织单位)。我只能看到用户的以下属性。
-
当我将用户从 OU1 移动到 OU2 时,
highestcommittedusn会增加,但是当我使用以下代码查询更改 (search.Filter = "(uSNChanged>=13000)") 时,我不会得到任何更新。 -
在 OU 中添加用户也是如此。
示例代码如下:
public static void GetUpdates()
{
var myLdapConnection = createDirectoryEntry();
var search = new DirectorySearcher(myLdapConnection);
search.Filter = "(uSNChanged>=13000)";
search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
var results = search.FindAll();
Console.WriteLine(results.Count);
}
public static DirectoryEntry createDirectoryEntry()
{
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://adfs.fed.abcd.com/DC=adfs,DC=fed,DC=abcd,DC=com");
ldapConnection.Path = "adfs.fed.abcd.com";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
private static long GetHighestUsn()
{
using (LdapConnection connection = new LdapConnection(ldapPath))
{
var filter = "(&(objectClass=*))";
var searchRequest = new SearchRequest(null, filter, System.DirectoryServices.Protocols.SearchScope.Base, "highestCommittedUSN");
var response = connection.SendRequest(searchRequest) as SearchResponse;
var usn = response.Entries[0].Attributes["highestcommittedusn"][0];
return Convert.ToInt64(usn);
}
return 0;
}
非常感谢任何帮助。
编辑:
- 我只有一个域控制器。
【问题讨论】:
标签: c# active-directory ldap directoryservices directorysearcher