We help IT Professionals succeed at work.

Javascript and LDAP

Tom Knowlton
Tom Knowlton asked
on
Can JavaScript interact with LDAP?

Here is some ASP.NET C# code -- is there a JavaScript equivalent?

  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";

            //But there is a StartTransportLayerSecurity()-method in the System.DirectoryServices.Protocols.LdapSessionOptions class ;)
            

            userName = "uid=coop-faculty_adoptions,ou=accounts,dc=uconn,dc=edu";
            passWord = "yinfarAusvig";
                        
            //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();            

                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

Comment
Watch Question

Multitechnician
SILVER EXPERT
Distinguished Expert 2019
Commented:
Tom KnowltonWeb developer

Author

Commented:
Why not directly?
leakim971Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented:
a browser can't use the LDAP protocol

Tom KnowltonWeb developer

Author

Commented:
ok
Tom KnowltonWeb developer

Author

Commented:
thx

Explore More ContentExplore courses, solutions, and other research materials related to this topic.