Click here to Skip to main content
15,068,929 members
Please Sign up or sign in to vote.
4.25/5 (4 votes)
See more:
Hi friends,



I'm trying the code to authenticate users using LDAP. I've tried many types of code but am not able to connect to the server. It's giving "the server is not operational" or "the server could not be reached". I read somewhere that port 445 should be open. Can anyone tell, is it mandatory that port 445 to be open? Mcode snippets is as follows:

1 st type
var host = "domain name here";

          using (LdapConnection ldap = new LdapConnection(host))
          {
              ldap.AuthType = AuthType.Basic;
              ldap.Bind(new NetworkCredential("cn=Manager,dc=maxcrc,dc=com", "newpas")); //
              SearchRequest searchRequest = new SearchRequest();
              searchRequest.DistinguishedName = "cn=Manager,dc=maxcrc,dc=co";
             /earchRequest.Filter = "(&(objectClass=user))";//"(&(objectClass=user))";
              SearchResponse response =
   (SearchResponse)ldap.SendRequest(searchRequest);

              if (response.Entries.Count == 1)
              {

              }

 }


2nd type
bool isAuthenticated = true;
          using (PrincipalContext pCtx = new PrincipalContext(ContextType.Domain,"LDAP://domain "))
          {
              isAuthenticated = pCtx.ValidateCredentials("dc = maxcrc, dc = com", "secret");
          }


3rd type
var credential = new NetworkCredential("dc=maxcrc,dc=com","newpass");

          using (var con = new LdapConnection(host) { Credential = credential, AuthType = AuthType.Anonymous, AutoBind = false })
          {
              con.SessionOptions.ProtocolVersion = 3;

         
              con.Bind();


}


4th type
 string LDAP_URL = "LDAPS://domain";
            DirectoryEntry entry = new DirectoryEntry(LDAP_URL);
            entry.RefreshCache();
            Object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(sAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("DisplayName");
            //search.PropertiesToLoad.Add("employeeID");
            search.PropertiesToLoad.Add("mail");
            //search.PropertiesToLoad.Add("SapPrimeDomText");
            SearchResult result = search.FindOne();

            if (result != null)
            {

}


I'm not even able to connect to the LDAP server .

Can anyone suggest to me how I can achieve this?

thanks darshan
Posted
Updated 14-Dec-16 18:11pm
v4

Before you waste your time with code, confirm that the LDAP server to which you are trying to connect is available.

Normally one connects to an LDAP server on port tcp/389, or LDAPs on tcp/636. Poprt 445 is not required for LDAP queries. It may be required for some native Active Directory connections.

Start by trying to the server at the command line.

telnet ldap.example.com 389


If Telnet can't establish a connection, you know why your code can't.

If you are able to connect, download and try a free LDAP browser tool.

If both of the above work, it's your code's fault.

Here's a snippet of code from the last time I used LDAP authentication:

C#
/// <summary>
/// Authenticates the specified UserID.
/// </summary>
/// <param name="UserID">The user ID.</param>
/// <param name="Password">The password.</param>
/// <returns>True upon successful authentication; otherwise false.</returns>
public bool Authenticate(string UserID, string Password)
{
    var L1 = ActivityLog.AddAsync("Authenticating user '{0}'", UserID);
    if (UserID == string.Empty || Password == string.Empty)
    {
        L1.Fail(new Exception("UserID and Password cannot be empty."));
        return false;
    }

    var credential = new NetworkCredential(UserID, Password, CoreSettings.Current.ActiveDirectoryDomain);
    var server = CoreSettings.Current.LDAPServer;
    // Use round-robin DNS if it's available.
    var ips = Dns.GetHostAddresses(server);
    foreach (var ip in ips)
    {
        var L2 = ActivityLog.AddAsync(Importances.Debug, "Connecting to LDAP server '{0} ({1})'", server, ip);
        using (var ldap = new LdapConnection(server))
        {
            try
            {
                ldap.Timeout = TimeSpan.FromSeconds(LdapTimeout);
                ldap.Bind(credential);
                L2.End();
                L1.End();
                return true;
            }
            catch (Exception ex)
            {
                L2.Fail(ex);
                throw;
            }
        }
    }
    return false;
}
   
v2
Comments
darshan559 11-Mar-13 12:13pm
   
Hi Yvan Rodrigues thanks for ur reply !!! I tried using ldap admin and it got connected ....thats y i m doubting on port numbers now !!!!
Yvan Rodrigues 11-Mar-13 12:19pm
   
I've added a connection example.
darshan559 12-Mar-13 3:55am
   
Hi what namespace i shud use for ActivityLog and CoreSettings.Current.ActiveDirectoryDomain??
thanks darshan
Yvan Rodrigues 12-Mar-13 9:58am
   
Just leave that logging stuff out. It was specific to the application from which that snippet came. If you are connecting to an Active Directory server, substitute it for CoreSettings.Current.ActiveDirectoryDomain, same idea for the server which should be the fully-qualified hostname or ip address of the server.
I found my answer here

http://alokkadu.wordpress.com/2010/12/24/the-server-is-not-operational-active-directory/#comment-81

You basically need to establish the trust between your machine and the AD Server. So just add the IP and the domain in the trusted sites as below.

Open your run. (Windows + R)
Type drivers
Go to etc folder
Open Hosts file in text editor
at the end add the ip address and the domain name of the Active Directory Machine.
That’s it. Your error is resolved..
   
Hi,
Im trying to connect LDAP With SSL encryption using the DSO object in UFT. When Im connecting the LDAP connection manually it connecting , but connecting the code via UFT im getting an error like "Server is not operational" . Im using VBS here.
The same code is worked for other connections but it doesn't have SSL.

But trying to connect SSL for other connection im getting the "Server is not operational " error. Kindly help me to resolve the issue.
Please find the sample code I have used

"Dim oDS: Set oDS = GetObject("LDAP:")
Dim oAuth: Set oAuth = oDS.OpenDSObject("LDAP://Ip address:1636/cn=xxx-adm,ou=Human,o=Admin","xxx-adm","Password",1)
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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