简体   繁体   English

动态获取当前LDAP路径

[英]Get current LDAP Path dynamically

I'm developing a library with C# and .NET Framework 4.0. 我正在使用C#和.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? 有没有办法动态获取"LDAP://DC=ic,DC=local"

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. 我使用了System.DirectoryServices.ActiveDirectory库,并使用DomainDomainController对象来查找您要查找的内容。

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. 然后,您只需调用DomainManager.DomainPath ,一切都初始化(它避免资源泄漏)或DomainName等。 Or RootPath, which is very useful to initialize the root DirectoryEntry for DirectorySearcher . 或者ROOTPATH,这是非常有用的初始化根DirectoryEntryDirectorySearcher

I hope this answers your question and could help. 我希望这能回答你的问题并提供帮助。

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: 是的,您正在寻找的是默认的命名上下文,该信息保存在RootDSE上下文中,这对所有域都是通用的:

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

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

You should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. 您应该检查System.DirectoryServices.AccountManagement (S.DS.AM)命名空间。 Read all about it here: 在这里阅读所有相关内容:

Basically, you can define a domain context and easily find users and/or groups in AD: 基本上,您可以定义域上下文并轻松查找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! 新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM