Search code examples
c#classmodelsldap-query

c# LDAP Populating a list with details from an LDAP query


I'm building an application where I need to have a list created for each instance of matches within an LDAP query.

It should work like so... if the user searches 'Smith' for the sn (surname) then all people with the last name 'Smith' will have their details populated in the list.

I can then use this list to generate details of a pick box that the user can choose from and subsequently this will be saved on whatever form they have used.

I'm trying to make this reusable across many applications in my solution so the search field and values to search are passed to this action (most likely will be suffixed with a wildcard) and then the list populated. The page that's called this action can save whatever details they require from the LDAP search, be it the firstname or the cn, whatever is required.

I can't seem to get my code looking correct though, my assumption would be it was similar to a function in JS but Visual studio is highlighting parts of my code as incorrect.

Here's my code (I've put comments next to parts that are highlighted by visual studio as incorrect)

using System.Collections.Generic;
using System.DirectoryServices;

namespace solutionName.Apps.Models
{
    public class LDAP
    {
        protected void LDAP_Search(string LDAP_Field, string LDAP_Value)
        {
            string _ldapserver = "myLDAPServer";
            string _port = "123";
            string _username = "myUserName";
            string _password = "myPassword";
            _ldapserver = "LDAP://" + _ldapserver + ":" + _port;
            DirectoryEntry entry = new DirectoryEntry(_ldapserver, _username, _password);
            entry.AuthenticationType = AuthenticationTypes.None;
            DirectorySearcher deSearch = new DirectorySearcher(entry);
            deSearch.PropertiesToLoad.Add("fullname");
            deSearch.PropertiesToLoad.Add("givenName");
            deSearch.PropertiesToLoad.Add("sn");
            deSearch.PropertiesToLoad.Add("PersonOUC");
            deSearch.PropertiesToLoad.Add("mail");
            deSearch.PropertiesToLoad.Add("cn");
            deSearch.Filter = "(&(" + LDAP_Field + "=" + LDAP_Value + "))";
            var LDAPResponses = new List<LDAP_Search_Model>
            { // this line is highlighted as incorrect
                foreach (SearchResult sresult in deSearch.FindAll())
                {
                    new LDAP_Search_Model
                    {
                        fName = sresult.Properties["givenName"][0].ToString(),
                        sName = sresult.Properties["sn"][0].ToString(),
                        fullName = sresult.Properties["fullname"][0].ToString(),
                        OUC = sresult.Properties["PersonOUC"][0].ToString(),
                        email = sresult.Properties["mail"][0].ToString(),
                        UIN = sresult.Properties["cn"][0].ToString()
                    };
                }
            }; // this line is also highlighted as incorrect

        }
    }

    public class LDAP_Search_Model
    {
        public string UIN { get; set; }
        public string fName { get; set; }
        public string sName { get; set; }
        public string fullName { get; set; }
        public string OUC { get; set; }
        public string email { get; set; }
    }


}

Solution

  • The following codes might fix the problem.

    var LDAPResponses = new List<LDAP_Search_Model>();
    foreach (SearchResult sresult in deSearch.FindAll())
    {
       LDAPResponses.Add(new LDAP_Search_Model
       {
           //...
       });
    };