Connect to the LDAP Active Directory in Java without username and password
<p>I have a simple application written in C#.
That application works on windows machine and user account logged to the Active Directory.</p>

<p>In that application I retrive some information from LDAP Active Directory.</p>

<p>In C# I do connection with LDAP in that way:</p>

<pre><code>DirectoryEntry de = new DirectoryEntry("LDAP://OU=places,DC=system,DC=org");
</code></pre>

<p>and next I retrieve needed data (for example):</p>

<pre><code>DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = "(telephone=111222333)";
SearchResultCollection resultCollection = searcher.FindAll();
</code></pre>

<p>As you see in that application I do not need log to the LDAP with username and password but only I need to put the correct path:</p>

<p>The constructor of <code>DirectoryEntry</code> looks like that:</p>

<pre><code>public DirectoryEntry(
    string path
)
</code></pre>

<p>Of course there is a constructor with username and password, but as I said I do not need to use it:</p>

<pre><code>public DirectoryEntry(
    string path,
    string username,
    string password
)
</code></pre>

<p>I tried to retrieve the same information in Java and I used the similar code:</p>

<pre><code>Hashtable&lt;String, String&gt; env = new Hashtable&lt;String, String&gt;();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ad.host:389");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");

DirContext ldap = new InitialDirContext(env);
</code></pre>

<p>and next I retrieve needed data (for example):</p>

<pre><code>SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration tel = ldap.search("OU=places,DC=system,DC=org", "(telephone=111222333)", searchControls);
</code></pre>

<p>But there is a one difference. As you see I need to put username and password in Java if I want to connect to that LDAP Active Directory.</p>

<p>If I do not put the username and password I will see the following error:</p>

<pre><code>Exception in thread "main" javax.naming.NamingException:[LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E8, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1;] remaining name 'OU=places,DC=system,DC=org'
</code></pre>

<p>My question is if can I log to the same LDAP Active Directory from the same machine in Java without need to put username and password?</p>

<p>Is there an equivalent to do this in Java like in C# (without put the username and password)?</p>