Question

I'm developing a library with C# and .NET Framework 4.0.

I want to retrieve all active directory users and it works great. But my problem if I run my program on another domain I have to change this:

private static string ldapPath = "LDAP://DC=ic,DC=local";

And recompile it with the new data for the new domain.

Is there any way to get "LDAP://DC=ic,DC=local" dynamically?

Was it helpful?

Solution

I've done the exact same thing few weeks ago. I used the System.DirectoryServices.ActiveDirectory library, and used the Domain and DomainController objects to find what you are looking for.

Here is the code I'm using:

public static class DomainManager
{
    static DomainManager()
    {
        Domain domain = null;
        DomainController domainController = null;
        try
        {
            domain = Domain.GetCurrentDomain();
            DomainName = domain.Name;
            domainController = domain.PdcRoleOwner;
            DomainControllerName = domainController.Name.Split('.')[0];
            ComputerName = Environment.MachineName;
        }
        finally
        {
            if (domain != null)
                domain.Dispose();
            if (domainController != null)
                domainController.Dispose();
        }
    }

    public static string DomainControllerName { get; private set; }

    public static string ComputerName { get; private set; }

    public static string DomainName { get; private set; }

    public static string DomainPath
    {
        get
        {
            bool bFirst = true;
            StringBuilder sbReturn = new StringBuilder(200);
            string[] strlstDc = DomainName.Split('.');
            foreach (string strDc in strlstDc)
            {
                if (bFirst)
                {
                    sbReturn.Append("DC=");
                    bFirst = false;
                }
                else
                    sbReturn.Append(",DC=");

                sbReturn.Append(strDc);
            }
            return sbReturn.ToString();
        }
    }

    public static string RootPath
    {
        get
        {
            return string.Format("LDAP://{0}/{1}", DomainName, DomainPath);
        }
    }
}

And then, You simply call DomainManager.DomainPath, everything is initialized once (it avoids resource leaks) or DomainName and so on. Or RootPath, which is very useful to initialize the root DirectoryEntry for DirectorySearcher.

I hope this answers your question and could help.

OTHER TIPS

Yes there is, what you are looking for is the default naming context, that information is held in the RootDSE context which is common to all domains:

DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");

string defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value;

You should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

Basically, you can define a domain context and easily find users and/or groups in AD:

// set up domain context - uses the current domain you're connected to
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

    if(user != null)
    {
       // do something here....     
    }
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top