We help IT Professionals succeed at work.
Get Started

Javascript and LDAP

Tom Knowlton
Tom Knowlton asked
on
687 Views
Last Modified: 2012-05-12
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
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE