none
C# DirectoryEntry does not connect to my active directory LDAP path

    Question

  • Whenever I input my credentials, I throws network not found exception

    machinename = test-machine:389/DC=test,DC=com (I verified my username and password and they are correct)

    private void button1_Click(object sender, EventArgs e)
        {
           string machinename = textBox1.Text;
           string name = textBox2.Text;
           string password = textBox3.Text;
    
    
            listView1.Items.Clear();
    
            if (machinename.Equals("") || name.Equals("") || password.Equals(""))
            {
                MessageBox.Show("Enter all values", "Free AD Tools Error Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
    
                //button7.Visible = true;
                string path = "LDAP://"+machinename;
                try
                {
                    DirectoryEntry de = new DirectoryEntry(path,name,password,AuthenticationTypes.Secure);
                    if (DirectoryEntry.Exists(path)) //my code fails here
                    { MessageBox.Show("connected"); }
                        try
                        {
                            DirectoryEntries userEntries = de.Children;
                           // MessageBox.Show("hello2");
                            foreach (DirectoryEntry users in userEntries)
                            {
                                if (users.SchemaClassName.Equals("User"))
                                {
    
                                    //   listView1.Items.Add(users.Name);
    
                                    ListViewItem item = new ListViewItem(users.Name, 0);
                                    item.SubItems.Add(users.Properties["cn"].Value.ToString());
                                    item.SubItems.Add(users.Properties["givenName"].Value.ToString());;
                                   // listView1.Items.AddRange(new ListViewItem[] { item });
                                    listView1.Items.Add(item);
    
                                }
    
                            }
                           // MessageBox.Show("hello2");
                           // button7.Visible = false;
                        }
                        catch (System.Runtime.InteropServices.COMException exe)
                        {
                            MessageBox.Show("Unknown user or bad password", "Free AD Tools Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                           // button7.Visible = false;
                        }
    
    
                    //}
                }
                catch (Exception exe)
                {
                    MessageBox.Show("Network Path Not Found", "Free AD Tools Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);//throws this exception
                    //button7.Visible = false;
                }
            }

    • Moved by Sabah ShariqMVP Thursday, April 6, 2017 9:35 PM Moved from Visual C#
    Wednesday, April 5, 2017 3:34 PM

All replies