代码之家  ›  专栏  ›  技术社区  ›  kakopappa

如何查询Active Directory中的更改,包括已删除的对象?

  •  4
  • kakopappa  · 技术社区  · 14 年前

    我正在使用下面的代码来查询对AD中用户/OU的更改。但是它不会检索任何已删除的对象,您知道如何将已删除的对象包含在其中吗?

    static void Main(string[] args)
            {
     BinaryFormatter bFormat = new BinaryFormatter();
                byte[] cookie = null;
                string strFileName = "cookie.bin";
                if (File.Exists(strFileName))
                {
                    using (FileStream fsStream = new FileStream(strFileName, FileMode.OpenOrCreate))
                    {
                        cookie = (byte[])bFormat.Deserialize(fsStream);
                    }
                }
    
    
                string str_dcName = "xxxxx"; 
                System.DirectoryServices.DirectoryEntry rootDSE = new System.DirectoryServices.DirectoryEntry("LDAP://rootDSE");
                System.Net.NetworkCredential cr = new System.Net.NetworkCredential(@"xxx", "xxx", "xxx");
                LdapConnection connection = new LdapConnection(str_dcName);
                connection.Credential = cr;
                connection.Bind();
    
                string[] attribs = new string[3];
                attribs[0] = "name";
                attribs[1] = "description";
                attribs[2] = "objectGUID";
    
                SearchRequest request = new SearchRequest("DC=xxx,DC=xxx,DC=com", "(objectClass=*)", SearchScope.Subtree, attribs);
    
                DirSyncRequestControl dirSyncRC = new DirSyncRequestControl(cookie, DirectorySynchronizationOptions.IncrementalValues, Int32.MaxValue);
                request.Controls.Add(dirSyncRC);
    
                bool bMoreData = true;
                SearchResponse searchResponse = (SearchResponse)connection.SendRequest(request);
    
                while (bMoreData) //Initial Search handler - since we're unable to combine with paged search
                {
                    foreach (SearchResultEntry entry in searchResponse.Entries)
                    {
                        System.Collections.IDictionaryEnumerator attribEnum = entry.Attributes.GetEnumerator();
                        while (attribEnum.MoveNext())//Iterate through the result attributes
                            {
                            //Attributes have one or more values so we iterate through all the values 
                            //for each attribute
                            DirectoryAttribute subAttrib = (DirectoryAttribute)attribEnum.Value;
                            for (int ic = 0; ic < subAttrib.Count; ic++) {
                                //Attribute Name below
                                Console.WriteLine(attribEnum.Key.ToString());
                                //Attribute Sub Value below
                                Console.WriteLine(subAttrib[ic].ToString());
                            }
                        }
                    }
    
                    //Get the cookie from the response to use it in next searches
    
    
                    foreach (DirectoryControl control in searchResponse.Controls)
                    {
                        if (control is DirSyncResponseControl)
                        {
                            DirSyncResponseControl dsrc = control as DirSyncResponseControl;
                            cookie = dsrc.Cookie;
                            bMoreData = dsrc.MoreData;
                            break;
                        }
                    }
                    dirSyncRC.Cookie = cookie;
                    searchResponse = (SearchResponse)connection.SendRequest(request);
                }
    
    
                //Serialize the cookie into a file to use in next searches
                using (FileStream fsStream = new FileStream(strFileName, FileMode.Create))
                {
                    //Serialize the data to the steam. To get the data for 
                    //the cookie, call the GetDirectorySynchronizationCookie method.
                   bFormat.Serialize(fsStream, cookie);
                }
    
                Console.WriteLine("Finished search...");
                Console.ReadKey(); 
    
               }
    
    2 回复  |  直到 9 年前
        1
  •  4
  •   Ocaso Protal    10 年前

    解决方案是需要添加 isDeleted=TRUE 请求对象

    SearchRequest request = new SearchRequest("DC=xx,DC=xxx,DC=com", 
      "(|(objectClass=organizationalUnit)(isDeleted=TRUE)(objectCategory=Person))",
      SearchScope.Subtree, attribs);
    
        2
  •  0
  •   Kevin M    11 年前

    至于我的经验与SearchRequest过滤器将不会工作在任何成本,它有自己的问题。。对于您的情况,要获得删除的对象更改,您应该使用域管理员特权帐户或类似的。。

    System.Net.NetworkCredential cr = new System.Net.NetworkCredential(@"administrator", "xxx", "xxx");
            LdapConnection connection = new LdapConnection(str_dcName);