Click here to Skip to main content
15,037,168 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi gusy , i have been encountering with one issue.
Anybody knows about how to check connection with LDAP server (C#) using only such parameters:
-Server name
- full domain
- port

In Java enviroment,i have created such helper class (it perform such verivication as i described above):
Java
public class LdapHelper
{
	//method from class LoginAPI::checkLDAP(...)
	public static boolean loginLDAP(String loginName, String password, String ldapServer, String ldapPort, String ldapDomainName)
			throws Exception
	{
		final LDAPAuthenticate ldapAuthenticate = LDAPAuthenticate.getInstance();
		ldapAuthenticate.init(
				"ldap",
				ldapServer,
				ldapPort,
				ldapDomainName
		);
		return ldapAuthenticate.loginUser( loginName, password );
	}

	//method from class LoginAPI::logoutLdap(...)
	public static void logoutLdap() throws Exception
	{
		final LDAPAuthenticate ldapAuthenticate = LDAPAuthenticate.getInstance();
		ldapAuthenticate.logout();
	}

	//Part of sources from CommonManager:: savePsParameters(...)
	public static boolean checkLdapServer(String serverName, String fullDomainName, String port)
	{
		return LDAPAuthenticate.getInstance().checkLdapServer(
				serverName,
				fullDomainName,
				port
		);
	}

	public static boolean loginSimple(String loginName, String password, String serverName, String fullDomainName, String port,
	                               String base) throws Exception
	{
		Hashtable authEnv = new Hashtable( 11 );

		String dn = "uid=" + loginName + "," + base;
		String ldapURL = "ldap://" + serverName + "." + fullDomainName.toUpperCase() + ":" + port;

		authEnv.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
		authEnv.put( Context.PROVIDER_URL, ldapURL );
		authEnv.put( Context.SECURITY_AUTHENTICATION, "simple" );
		authEnv.put( Context.SECURITY_PRINCIPAL, dn );
		authEnv.put( Context.SECURITY_CREDENTIALS, password );

		DirContext authContext = new InitialDirContext( authEnv );
		System.out.println( "Authentication Success!" );

		return true;
	}
}


All answers and suggestions are acceptable =)

Well i be more precious, i need to check in LDAP context such string , see below:
"LDAP://" + ldapServer + "."+DomainName.trim() + ":" + port.trim() + "/"
Posted
Updated 8-Aug-12 2:37am
v2

C#
DirectoryEntry objDE = new DirectoryEntry("LDAP://XXX.XXX.XXX.XXX", "domain\\login", "password");
          using (objDE)
          {
              DirectorySearcher objDSearcher = new DirectorySearcher();
              objDSearcher.SearchRoot = objDE;
              objDSearcher.PropertiesToLoad.Add("department");
              objDSearcher.PropertiesToLoad.Add("title");
              objDSearcher.PropertiesToLoad.Add("cn");

              objDSearcher.PropertiesToLoad.Add("SAMAccountName");
              objDSearcher.PropertiesToLoad.Add("givenname");

              objDSearcher.PropertiesToLoad.Add("sn");
              objDSearcher.PropertiesToLoad.Add("memberOf");

              objDSearcher.PropertiesToLoad.Add("department");
              objDSearcher.PropertiesToLoad.Add("title");

              objDSearcher.PropertiesToLoad.Add("postalCode");
              objDSearcher.PropertiesToLoad.Add("streetAddress");

              objDSearcher.PropertiesToLoad.Add("st");
              objDSearcher.PropertiesToLoad.Add("telephoneNumber");

              objDSearcher.PropertiesToLoad.Add("l");
              objDSearcher.PropertiesToLoad.Add("mail");
              //objDSearcher.Filter = "(&(department=GTS Miscellaneous)(title=SOFTWARE ENGINEER))";
              //objDSearcher.Filter = "(&(department=GTS Telecom))";
              objDSearcher.Filter = "(SAMAccountName=SB5817)";

              objDSearcher.SearchScope = SearchScope.Subtree;
              try
              {
                  SearchResultCollection result = objDSearcher.FindAll();
                  foreach (SearchResult sr in result)
                  {

                      Response.Write("ID:" + sr.Properties["SAMAccountName"][0].ToString() + "<br/>");
                      Response.Write("ID:" + sr.Properties["givenname"][0].ToString() + "<br/>");
                      Response.Write("ID:" + sr.Properties["cn"][0].ToString() + "<br/>");
                      Response.Write("Department:" + sr.Properties["Department"][0].ToString() + "<br/>");
                      Response.Write("title:" + sr.Properties["title"][0].ToString() + "<br/>");
                      Response.Write("------------------------------------------------------------------------<br/>");
                  }
              }
              catch (System.DirectoryServices.DirectoryServicesCOMException ex)
              {
                  Response.Write(ex.Message);
              }
              catch (Exception ex)
              {
                  Response.Write(ex.Message);
              }
   
Comments
Oleksandr Kulchytskyi 8-Aug-12 8:22am
   
Yep, thanks for it.
But it require a login and password.But in my case, i can only provide it with
server name, full domain name and port.
So this is not exactly what i need.
Oleksandr Kulchytskyi 8-Aug-12 8:34am
   
Well exactly i need to check access to following string :
Ldap + "://" + ldapServer + fulldomain.trim() + ":" + port.trim() + "/"
I have resolved this issue by myself.
See method below:

C#
public bool checkLdapServer(string server, string domain, int port)
        {
            try
            {
                using (DirectoryEntry dirEnt = new DirectoryEntry("LDAP://" + server.Trim() + "." + domain.Trim() + ":" + port.ToString()))
                {
                    dirEnt.AuthenticationType = AuthenticationTypes.None;
                    using (var searcher = new DirectorySearcher(dirEnt))
                    {
                        searcher.SearchScope = SearchScope.OneLevel;
                        SearchResult searchResult = searcher.FindOne();
                        dirEnt.Close();
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900