David Morrison
David Morrison

Reputation: 11

AD DirSync need the old value of attribute as well as the new

I've been searching high and low and I can't find an example of how to get the old value of a changed attribute in ADLDS. I've created a web service to monitor for changes using Change Notifications, and that works fine. I'm able to get adds, changes and deletions. Now however, I need to be able to give the old and new attribute values for changes. I'm thinking I need to use DirSync as it will only return the attributes that have changed. However, it only seems to give me the new value. Is there anyway to return what the old attribute value was? Just in case this helps, here is the DirSync code I'm currently using:

        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 = "xx.x.xx.xx:5000";

        LdapConnection connection = new LdapConnection(str_dcName);

        connection.SessionOptions.ProtocolVersion = 3;
        connection.Credential = new System.Net.NetworkCredential("CN=administrator,ou=blah,dc=blahblah", "password");
        connection.AuthType = AuthType.Basic;
        connection.Bind();

        string[] attribs = new string[4];
        attribs[0] = "sn";
        attribs[1] = "isDeleted";
        attribs[2] = "displayname";

        SearchRequest request = new SearchRequest("OU=blah,DC=blahblah", "(|(objectClass=*)(isDeleted=TRUE))", 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();

Thanks for any help!!

Dave

Upvotes: 1

Views: 249

Answers (0)

Related Questions