Пример #1
0
        public override void Export(string filename)
        {
            using (LdapConnection connect = new LdapConnection(new LdapDirectoryIdentifier(Server, Port == 0 ? 389 : Port), Credential))
            {
                var    filter        = "(&(objectClass=*))";
                var    searchRequest = new SearchRequest(null, filter, SearchScope.Base);
                var    response      = connect.SendRequest(searchRequest) as SearchResponse;
                string root          = (string)response.Entries[0].Attributes["defaultNamingContext"][0];
                connect.Bind();

                using (StreamWriter sw = File.CreateText(filename))
                {
                    var header = new List <string>();
                    header.Add("Date");
                    header.Add("DistinguishedName");
                    header.Add("Attribute");
                    header.Add("Value");

                    sw.WriteLine(string.Join("\t", header.ToArray()));


                    EventHandler <ObjectChangedEventArgs> callback =
                        (object sender, ObjectChangedEventArgs e) =>
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        DisplayAdvancement(e.Result.DistinguishedName);
                        Console.ResetColor();
                        foreach (string attributeName in e.Result.Attributes.AttributeNames)
                        {
                            foreach (byte[] item in e.Result.Attributes[attributeName].GetValues(typeof(byte[])))
                            {
                                string i;
                                // there is no easy way to know the synthax of the object
                                // see https://social.technet.microsoft.com/wiki/contents/articles/52570.active-directory-syntaxes-of-attributes.aspx

                                // so we try each well known type one by one
                                try
                                {
                                    i = new SecurityIdentifier(item, 0).Value;
                                }
                                catch
                                {
                                    try
                                    {
                                        i = new Guid(item).ToString();
                                    }
                                    catch
                                    {
                                        try
                                        {
                                            i = Encoding.UTF8.GetString(item);
                                            for (int j = 0; j < i.Length; j++)
                                            {
                                                var ch  = i[j];
                                                var ich = (int)ch;
                                                if (ich > 127 || ich < 31)     // not ascii or extended ascii
                                                {
                                                    i = BitConverter.ToString(item);
                                                    break;
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            i = BitConverter.ToString(item);
                                        }
                                    }
                                }

                                //Console.WriteLine("{0}: {1}", attributeName, i);
                                var data = new List <string>();
                                data.Add(DateTime.Now.ToString("u"));
                                data.Add(e.Result.DistinguishedName);
                                data.Add(attributeName);
                                data.Add(i);
                                sw.WriteLine(string.Join("\t", data.ToArray()));
                                sw.Flush();
                            }
                        }
                    };


                    using (ChangeNotifier notifier = new ChangeNotifier(connect))
                    {
                        //register some objects for notifications (limit 5)
                        notifier.Register(root, SearchScope.Subtree);

                        notifier.ObjectChanged += callback;

                        DisplayAdvancement("Waiting for changes...");
                        DisplayAdvancement("Press ENTER to stop monitoring the changes");
                        Console.ReadLine();
                    }
                }

                DisplayAdvancement("Done");
            }
        }