Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
QuestionLooking for help querying active directory Pin
turbosupramk323-May-12 9:08
turbosupramk323-May-12 9:08 
AnswerRe: Looking for help querying active directory Pin
Dave Kreskowiak23-May-12 9:58
mveDave Kreskowiak23-May-12 9:58 
GeneralRe: Looking for help querying active directory Pin
turbosupramk323-May-12 10:23
turbosupramk323-May-12 10:23 
GeneralRe: Looking for help querying active directory Pin
Dave Kreskowiak23-May-12 12:09
mveDave Kreskowiak23-May-12 12:09 
GeneralRe: Looking for help querying active directory Pin
turbosupramk323-May-12 14:29
turbosupramk323-May-12 14:29 
AnswerRe: Looking for help querying active directory Pin
randprin24-May-12 2:16
randprin24-May-12 2:16 
GeneralRe: Looking for help querying active directory Pin
turbosupramk324-May-12 9:33
turbosupramk324-May-12 9:33 
GeneralRe: Looking for help querying active directory Pin
turbosupramk329-May-12 4:35
turbosupramk329-May-12 4:35 
Here is what I ended up using in case this comes up in a search (you'll have to change the txtbox stuff to fit your gui or console app


C#
using (var rootDirectory = new DirectoryEntry("LDAP://" + domain))//, "[user name in the directory]", "[User Password]"))
                    {
                        using (var directorySearch = new DirectorySearcher(rootDirectory))
                        {
                            if (tBoxPin.Text != "")
                            {
                                directorySearch.Filter = string.Format("(|(sAMAccountName={0}))", tBoxPin.Text);
                            }
                            if (tBoxLastName.Text != "")
                            {
                                directorySearch.Filter = string.Format("((sn={0}))", tBoxLastName.Text);

                            }
                            if (tBoxEmail.Text != "")
                            {
                                directorySearch.Filter = string.Format("((mail={0}))", tBoxEmail.Text);
                            }

                            var result = directorySearch.FindOne();

                            if (tBoxLastName.Text != "")
                            {
                                SearchResultCollection searchResults = directorySearch.FindAll();
                                foreach (SearchResult searchResult in searchResults)
                                {
                                    foreach (string propertyKey in searchResult.Properties.PropertyNames)
                                    {
                                        ResultPropertyValueCollection valueCollection = searchResult.Properties[propertyKey];
                                        ResultPropertyValueCollection accountCollection = searchResult.Properties["userprincipalname"];
                                        foreach (Object propertyValue in valueCollection)
                                        {
                                            if (propertyKey == "name")
                                            {
                                                lastNames.Add(propertyValue.ToString());
                                                if (tBoxLastName.Text != "")
                                                {
                                                    tBoxDomains.Text += (propertyValue.ToString()) + " - ";
                                                    foreach (Object account in accountCollection)
                                                    {
                                                        tBoxDomains.Text += account.ToString() + System.Environment.NewLine;
                                                        //MessageBox.Show(account.ToString());
                                                    }
                                                }
                                            }
                                        }

                                        //ResultPropertyValueCollection valueCollection = searchResult.Properties["name"];
                                        //lastNames.Add(item);
                                    }
                                }
                            }


                            //foreach (SearchResult item in searchResults)
                            //{
                            //    tBoxDomains.Text += item.Properties.PropertyNames + System.Environment.NewLine;
                            //}

                            if (lastNames.Count > 1)
                            {
                                return;
                            }

                            if (result != null)
                            {
                                DirectoryEntry myDirectoryEntry = result.GetDirectoryEntry();

                                ResultPropertyCollection myResultPropColl;
                                myResultPropColl = result.Properties;

                                foreach (string myKey in myResultPropColl.PropertyNames)
                                {

                                    if ((myKey == "samaccountname"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            tBoxPin.Text = (myCollection.ToString());
                                        }
                                    }

                                    if ((myKey == "name"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            tBoxLastName.Text = (myCollection.ToString());
                                        }
                                    }

                                    if ((myKey == "mail"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            tBoxEmail.Text = (myCollection.ToString());
                                        }
                                    }

                                    if ((myKey == "name"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            tBoxTopSecret.Text = (myCollection.ToString().ToUpper());
                                        }
                                    }

                                    if ((myKey == "homemdb"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            int delimiter = myCollection.ToString().IndexOf(",");
                                            string trimmed = myCollection.ToString();
                                            trimmed = trimmed.Remove(delimiter);
                                            trimmed = trimmed.Replace("CN=", "");
                                            tBoxExchangeServer.Text = (trimmed);
                                        }
                                    }

                                    if ((myKey == "telephonenumber"))
                                    {
                                        foreach (Object myCollection in myResultPropColl[myKey])
                                        {
                                            tBoxPhone.Text = (myCollection.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

QuestionUsing ADWS in .Net Pin
Member 254690323-May-12 6:40
Member 254690323-May-12 6:40 
AnswerRe: Using ADWS in .Net Pin
Ravi Bhavnani23-May-12 9:33
professionalRavi Bhavnani23-May-12 9:33 
GeneralRe: Using ADWS in .Net Pin
Member 254690323-May-12 9:59
Member 254690323-May-12 9:59 
GeneralSystem.IO Project for beginners Pin
bdeklerk23-May-12 2:28
bdeklerk23-May-12 2:28 
GeneralRe: System.IO Project for beginners Pin
ddecoy23-May-12 3:31
ddecoy23-May-12 3:31 
AnswerRe: System.IO Project for beginners Pin
Eddy Vluggen23-May-12 10:42
professionalEddy Vluggen23-May-12 10:42 
QuestionCrystal reports not working on win 7 - 64bit. help Pin
Waqas Ahmad Abbasi22-May-12 19:38
Waqas Ahmad Abbasi22-May-12 19:38 
AnswerRe: Crystal reports not working on win 7 - 64bit. help Pin
Bernhard Hiller22-May-12 22:10
Bernhard Hiller22-May-12 22:10 
AnswerRe: Crystal reports not working on win 7 - 64bit. help Pin
Sandeep Mewara22-May-12 23:38
mveSandeep Mewara22-May-12 23:38 
QuestionProblem in merging two files -pcl file Pin
shamypnr22-May-12 19:15
shamypnr22-May-12 19:15 
AnswerRe: Problem in merging two files - images Pin
Richard MacCutchan22-May-12 22:43
mveRichard MacCutchan22-May-12 22:43 
Questionsoftware copy protection Pin
Alex Nguyen1222-May-12 12:35
Alex Nguyen1222-May-12 12:35 
AnswerRe: software copy protection Pin
Dave Kreskowiak22-May-12 13:46
mveDave Kreskowiak22-May-12 13:46 
GeneralRe: software copy protection Pin
Bernhard Hiller22-May-12 22:18
Bernhard Hiller22-May-12 22:18 
AnswerRe: software copy protection Pin
Eddy Vluggen23-May-12 0:25
professionalEddy Vluggen23-May-12 0:25 
GeneralRe: software copy protection Pin
Bernhard Hiller23-May-12 4:02
Bernhard Hiller23-May-12 4:02 
AnswerRe: software copy protection Pin
Eddy Vluggen23-May-12 9:02
professionalEddy Vluggen23-May-12 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.