我在Active Directory中运行查询并使用DirectorySynchronization class轮询更改。
我需要确定自上次查询以来对象发生了哪种操作,即对象是否已创建,修改或删除。
我可以通过检查" isDeleted"来轻松确定对象是否已被删除。属性(需要在搜索器上设置Tombstone
属性),但我不知道如何确定对象是新对象还是已修改对象。理想情况下,我想通过查看查询结果来做到这一点。
这是一个(快速而又脏的)代码示例:
using System.DirectoryServices;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class Program {
static void Main(string[] args) {
string[] propertiesToLoad = new[] { "distinguishedName", "isDeleted" };
using (DirectoryEntry root = new DirectoryEntry("LDAP://someDC.domain.com/DC=domain,DC=com"))
using (DirectorySearcher searcher = new DirectorySearcher(root, "(objectClass=user)", propertiesToLoad)) {
searcher.SearchScope = SearchScope.Subtree;
searcher.Tombstone = true;
if (File.Exists("cookie.bin")) {
searcher.DirectorySynchronization = new DirectorySynchronization(
DirectorySynchronizationOptions.ObjectSecurity,
LoadCookie("cookie.bin"));
} else {
searcher.DirectorySynchronization = new DirectorySynchronization(
DirectorySynchronizationOptions.ObjectSecurity);
}
foreach (SearchResult result in searcher.FindAll()) {
if (result.Properties.Contains("isDeleted") && (bool)(result.Properties["isDeleted"][0])) {
// object is deleted
} else {
// HOW TO DETERMINE IF OBJECT WAS CREATED OR MODIFIED?
}
}
SaveCookie("cookie.bin", searcher.DirectorySynchronization);
}
}
static byte[] LoadCookie(string path) {
using (FileStream fs = new FileStream(path, FileMode.Open)) {
BinaryFormatter formatter = new BinaryFormatter();
return (byte[])formatter.Deserialize(fs);
}
}
static void SaveCookie(string path, DirectorySynchronization sync) {
using (FileStream fs = new FileStream(path, FileMode.Create)) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, sync.GetDirectorySynchronizationCookie());
}
}
}
答案 0 :(得分:0)
您可以尝试使用whenCreated和whenChanged属性。