我正在尝试设置DirSync control。以前我[已成功]使用了System.DirectoryServices.Protocols中的方法,但我发现它返回的结果只是部分对象 - 即使我定义了,我也无法从用户返回homeDrive属性它在SearchRequest的Attributes属性中。
因此,我正在尝试使用System.DirectoryServices在some of the documentation和examples之后设置DirSync。我成功连接到我的测试服务器(只能通过IP访问),我成功地只针对一个OU并搜索用户,如下所示:
byte[] cookie = null;
root = new DirectoryEntry(
"LDAP://[MyIPHere]/OU=test ou,DC=company,DC=com", "username", "password");
//Section A - Use this section for a regular search
DirectorySearcher src = new DirectorySearcher(root);
src.SearchScope = SearchScope.Subtree;
src.Filter = "(&(objectClass=user)(sAMAccountName=myuserhere)";
//Section B - Use this section for a DirSync
//src.DirectorySynchronization = new DirectorySynchronization(
DirectorySynchronizationOptions.IncrementalValues, cookie);
//src.Filter = "(&(objectCategory=person)(objectClass=user))";
//Execute the code whichever section is used
SearchResultCollection result = src.FindAll();
int count = result.Count;
Console.WriteLine(count.ToString());
foreach (SearchResult res in result)
{
//do things
}
但是,当我尝试使用B节而不是A节时,我在设置int count 的行上出现错误。 (我已经尝试将没有参数传递给src.DirectorySynchronization的构造函数,如示例中所示,结果相同):
COMException未处理
访问被拒绝。
当我尝试访问结果对象的属性或尝试迭代时,我只收到错误。如果我在 int count 行设置断点并查看结果对象,我会在结果的Count的值列中看到以下内容:
'result.Count'抛出了类型
的异常'System.Runtime.InteropServices.COMException'
我确保我的帐户对指定的OU和整个测试域都具有Replicating Directory Changes安全访问权限(以及所有其他安全访问权限)。我也尝试过使用单独的域管理员帐户。
如果我尝试在我们的生产域上运行它,在构造DirectoryEntry
对象时不传递凭据,我会遇到同样的问题。
考虑到我可以成功检索其他搜索结果,导致访问问题的这个DirectorySynchronization是什么,为什么在我调用src.FindAll()
时它不会发生?
(我对其他选项持开放态度,但我现在想避免使用USNChanged跟踪方法,因为它会返回完整的对象,并且我需要额外的编码。)
答案 0 :(得分:1)
DirSync搜索的基础必须是目录分区的根,即" DC = company,DC = com"在你的情况下。
见http://msdn.microsoft.com/en-us/library/ms677626(v=vs.85).aspx
"搜索的基础"在表中。
DirSync的一些更好的C#示例:
http://msdn.microsoft.com/en-us/magazine/cc188700.aspx#S1
请参阅"使用DirectorySearcher找到自己的方式"。
如果您只想跟踪OU /容器上的更改,是的,您必须使用USNChange。