authentication issue when using 3 parameter DirectoryContext constructor

Jul 7, 2015 at 9:55 AM
Hi

I'm tryng to get started with linq2ldap but ran into a snag right from the getgo. If I use the parameterless contstructor of DirectoryContext, or the one where I pass just a config, things work out fine. However, if I use the 3 parameter constructor, then I get an exception telling me the credentials are invalid. Sure enough if I check things out in Wireshark, the credentials that are being passed are from ly local user, not the credentials I specify.

This is the working code:
var cred = new NetworkCredential("nxodev\\Administrator", "mypass");

            string serverName = "nxodev.intra";
            var config = new LdapConfiguration().UseStaticStorage();
            var connectionFactory = config.ConfigurePooledFactory(serverName).AuthenticateAs(cred).AuthenticateBy(AuthType.Basic);

            string directoryOu = "DC=nxodev,DC=intra";
            string filter = "(&(telephoneNumber=*))";

            using (var context = new DirectoryContext())
            {
                List<IDirectoryAttributes> users = context.Query(directoryOu).Where(filter).InPagesOf(10);
            }
Likewise, this works fine
            using (var context = new LinqToLdap.DirectoryContext(config))
            {
                List<IDirectoryAttributes> users = context.Query(directoryOu).Where(filter).InPagesOf(10);
            }
However, this doesn't:
var conn = new LdapConnection(serverName);
            using (var context = new LinqToLdap.DirectoryContext(conn, true, config)) 
            {
                List<IDirectoryAttributes> users = context.Query(directoryOu).Where(filter).InPagesOf(10);
            }
Now I presume that it will use the credentials frmo the Ldapconnection I'm creating on the fly. So I modified the code to create the connection as follows
            LdapDirectoryIdentifier id = new LdapDirectoryIdentifier(serverName);
            var conn = new LdapConnection(id, cred);
and then perform the query in the same way.. but it doesn't change anything.

Any idea on what is happening here?
Coordinator
Jul 24, 2015 at 4:53 PM
Can you try performing a bind operation against the LdapConnection and see if it succeeds with your credentials?
Jul 28, 2015 at 12:50 PM
If I take the first non working example and add
conn.Bind(cred);
Than the subsequent query on the context works. if I do a Bind with no parameters, I get an exception telling me that my credentials are invalid.

Full working code (remove cred from conn.Bind and it stops working).
var conn = new LdapConnection(serverName);
            conn.Bind(cred);
            using (var context = new LinqToLdap.DirectoryContext(conn, true, config)) 
            {
                List<IDirectoryAttributes> users = context.Query(directoryOu).Where(filter).InPagesOf(10);
            }
And for the use of LdapDirectoryIdentifier, I got it working usiing
LdapDirectoryIdentifier id = new LdapDirectoryIdentifier(serverName);
            var conn = new LdapConnection(id, cred);
            using (var context = new LinqToLdap.DirectoryContext(conn, true, config)) 
            {
                List<IDirectoryAttributes> users = context.Query(directoryOu).Where(filter).InPagesOf(10);
            }