n00b question....how do you wire this up to your actual AD information?

May 8, 2013 at 9:37 PM
I have the .dll referenced in my project and the sample directorycontextmock .cs file in my project. I understand LINQ fairly well from using LINQ to SQL to access data in SQL Server. I understand that normally the .dbml file has the smarts on how to get to the database, etc.

So how do you get the directorycontextmock to know about your AD information so that you can write queries, etc.?

Sample code is definitely appreciated. Thank you for your time.
Coordinator
May 9, 2013 at 10:15 PM
@dwdrumz

Thanks for using the framework.

The base DirectoryContext object has 4 overloads for the constructor.
        public DirectoryContext()  : this(GetLdapConnectionString())
        {
        }
        public DirectoryContext(string connectionString) : this(new DirectoryEntry(connectionString))
        {
            ConnectionString = connectionString;
        }
        public DirectoryContext(string connectionString, string userName, string password) : this(new DirectoryEntry(connectionString, userName, password))
        {
            ConnectionString = connectionString;
        }
        public DirectoryContext(DirectoryEntry domainEntry)
        {
            DomainEntry = domainEntry;
            domainEntry.AuthenticationType = AuthenticationTypes.Secure;
        }
1) The first, (empty) constructor uses the RootDSE to find the current domain and connection string and uses the current user the process is running as to authenticate with.
        public static string GetLdapConnectionString()
        {
            using (var adRoot = new DirectoryEntry("LDAP://RootDSE"))
            {
                var dnc = Convert.ToString(adRoot.Properties["defaultNamingContext"][0]);
                var server = Convert.ToString(adRoot.Properties["dnsHostName"][0]);
                return string.Format("LDAP://{0}/{1}", server, dnc);
            }
        }
2) The second (taking a connectionString) uses the connection string provided to find the domain controller to connect to and the same user as constructor as #1.
"LDAP://[ServerName]/dc=contoso,dc=com"
3) The third (connecitonString, username, password) uses the connection string provided as well as the username and password to authenticate with.

4) The fourth (taking a System.DirectoryServices.DirectoryEntry) object allows you to construct your own reference to the domain controller, that can be configured to be pointing at a sub OU (organizational unit) within the domain.

Hope this helps.

Thanks,
Stephen Baker
May 10, 2013 at 1:08 PM
Perfect! This helps a bunch. Thank you for your time and effort.