Link to home
Create AccountLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

LDAP: "there is no such object on the server"

Trying to connect to an LDAP server, and I get this message back:

"there is no such object on the server"


Here is my current attempt via C# code:

        public XmlDocument GetLDAPInfo(string filter)
        {
            XmlDocument xd = new XmlDocument();
            string domain = string.Empty;
            string userName = string.Empty;
            string passWord = string.Empty;
            AuthenticationTypes at = AuthenticationTypes.SecureSocketsLayer;
            StringBuilder sb = new StringBuilder();

            //****Connecting to LDAP active directory
          
            domain = @"LDAP://ldap.uits.uconn.edu";

                     

            userName = "uid=coop-faculty_adoptions,ou=accounts,dc=uconn,dc=edu";
            passWord = "xxxxxxxxxxxxxxxxxxxx";
                        
            //Create the object necessary to read the info from the LDAP directory
            DirectoryEntry entry = new DirectoryEntry(domain, userName, passWord, at);

            Response.Write(entry.Properties.Count.ToString());


            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            SearchResultCollection results;
            mySearcher.Filter = filter;

            try
            {
                results = mySearcher.FindAll();  //ERROR ... ENTERS CATCH BLOCK AFTER THIS LINE EXECUTES

                if (results.Count > 0)
                {
                    Response.Write(results.Count.ToString());


                    sb.Append("<Users>");
                    foreach (SearchResult resEnt in results)
                    {
                        sb.Append("<User>");
                        ResultPropertyCollection propcoll = resEnt.Properties;
                        foreach (string key in propcoll.PropertyNames)
                        {
                            foreach (object values in propcoll[key])
                            {
                                switch (key)
                                {
                                    case "sn":
                                        sb.Append("<surname>" + values.ToString() + "</surname>");
                                        break;
                                    case "cn":
                                        sb.Append("<cn>" + values.ToString() + "</cn>");
                                        break;
                                    case "name":
                                        sb.Append("<name>" + values.ToString() + "</name>");
                                        break;
                                    case "givenname":
                                        sb.Append("<givenname>" + values.ToString() + "</givenname>");
                                        break;
                                    case "distinguishedname":
                                        sb.Append("<distinguishedname>" + values.ToString() + "</distinguishedname>");
                                        break;
                                    case "member":
                                        sb.Append("<member>" + values.ToString() + "</member>");
                                        break;
                                    case "initials":
                                        sb.Append("<initials>" + values.ToString() + "</initials>");
                                        break;
                                    case "postalcode":
                                        sb.Append("<postalcode>" + values.ToString() + "</postalcode>");
                                        break;
                                    case "l":
                                        sb.Append("<location>" + values.ToString() + "</location>");
                                        break;
                                    case "c":
                                        sb.Append("<c>" + values.ToString() + "</c>");
                                        break;
                                    case "mobile":
                                        sb.Append("<mobile>" + values.ToString() + "</mobile>");
                                        break;
                                    case "homephone":
                                        sb.Append("<homephone>" + values.ToString() + "</homephone>");
                                        break;
                                    case "title":
                                        sb.Append("<title>" + values.ToString() + "</title>");
                                        break;
                                    case "co":
                                        sb.Append("<co>" + values.ToString() + "</co>");
                                        break;
                                    case "st":
                                        sb.Append("<state>" + values.ToString() + "</state>");
                                        break;
                                    case "mail":
                                        sb.Append("<mail>" + values.ToString() + "</mail>");
                                        break;
                                    case "password":
                                        sb.Append("<password>" + values.ToString() + "</password>");
                                        break;
                                    case "samaccountname":
                                        sb.Append("<samaccountname>" + values.ToString() + "</samaccountname>");
                                        break;
                                    case "memberof":
                                        sb.Append("<memberof>" + values.ToString() + "</memberof>");
                                        break;
                                    case "uid":
                                        sb.Append("<userid>" + values.ToString() + "</userid>");
                                        break;
                                    case "description":
                                        sb.Append("<description>" + values.ToString() + "</description>");
                                        break;
                                }
                            }
                        }
                        sb.Append("</User>");
                    }
                    sb.Append("</Users>");
                    xd.LoadXml(sb.ToString());
                    return xd;
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            sb.Append("<Users><User>None</User></Users>");
            xd.LoadXml(sb.ToString());
            return xd;
        }

Open in new window




the filter being passed into the method is:

uid=abc123

(replacing abc123 with an actual netid does work in a windows client I downloaded, but not in the code above)

I should also note that I WAS able to connect via a windows LDAP client found on the web here:

http://ldapadmin.sourceforge.net/


So the LDAP server is able to give me the information...   there is a problem with my C# code or the way I am requesting the info.


Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

1. If you are using .Net 3.5 I would recommend you to use System.DirectoryServices.AccountManagement namespace.

2. If you are using .Net 2.0 follow steps (as I have implemented for our website):

2.1. First create connection string in web.config:

<add name="ADConnection" connectionString="LDAP://XXX.ldap.uits.uconn.edu/OU=accounts,DC=ldap,DC=uits,DC=uconn,DC=edu"/>

Open in new window


where XXX stands for  Domain Controller of AD ( Get it by typing echo % LOGONSERVER % in AD Computer's command prompt)

2.2. For AD Username and Password you have to use same that are used for logging in AD.

2.3. Use the following code for getting users from AD:

Set adminPath to connection string value for AD
 DirectoryEntry AD = new DirectoryEntry(adminPath, adUser, adPass, AuthenticationTypes.Secure);

        try
        {
            using (DirectorySearcher ds = new DirectorySearcher(AD))
            {
                ds.Filter = "DisplayName=" + adUser;

                SearchResult sr = ds.FindOne();
                if (sr != null)
                {
                    user = "User Found";
                }
            }
        }

Open in new window


Hope it help you to get connected to AD. Let us know if unable to achieve objective.
Avatar of Tom Knowlton

ASKER

I am sorry, but I do not understand what you are asking me to do.

I need to know why my code gets this error from the LDAP server:

"there is no such object on the server"


When the windows application can connect just fine:

http://ldapadmin.sourceforge.net/
ASKER CERTIFIED SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I tried making the change....it made no difference... same error still.


"there is no such object on the server"
It was something along these lines.
NOTE:

Here is my final working code:


  public static bool PersonFoundInLDAPIsFacultyMember(string filter)
        {
            string domain = string.Empty;
            string userName = string.Empty;
            string passWord = string.Empty;
            AuthenticationTypes at = AuthenticationTypes.SecureSocketsLayer;
            StringBuilder sb = new StringBuilder();

            //****Connecting to LDAP active directory          
            domain = @"LDAP://ldap.uits.uconn.edu/dc=uconn,dc=edu";        

            userName = "uid=coop-faculty_adoptions,ou=accounts,dc=uconn,dc=edu";
            passWord = "you wish";

            //Create the object necessary to read the info from the LDAP directory
            DirectoryEntry entry = new DirectoryEntry(domain, userName, passWord, at);        
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            SearchResultCollection results;
            mySearcher.Filter = filter;

            bool facultymemfound = false;

            try
            {
                results = mySearcher.FindAll();

                if (results.Count > 0)
                {                   
                    foreach (SearchResult resEnt in results)
                    {                      
                        ResultPropertyCollection propcoll = resEnt.Properties;
                        foreach (string key in propcoll.PropertyNames)
                        {
                            foreach (object values in propcoll[key])
                            {
                                switch (key)
                                {
                                    case "uconnpersonaffiliation":
                                        if (values.ToString() == "Professional Staff")
                                        {
                                            facultymemfound = true;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }                        
                    }                    
                }
            }
            catch (Exception ex)
            {                
            }

            return facultymemfound;
        }

Open in new window