【问题标题】:Get current LDAP Path dynamically动态获取当前 LDAP 路径
【发布时间】:2014-04-30 08:33:04
【问题描述】:

我正在使用 C# 和 .NET Framework 4.0 开发一个库。

我想检索所有活动目录用户,而且效果很好。但是我的问题是,如果我在另一个域上运行我的程序,我必须更改它:

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

并使用新域的新数据重新编译它。

有没有办法动态获取"LDAP://DC=ic,DC=local"

【问题讨论】:

    标签: c# windows active-directory


    【解决方案1】:

    几周前我也做过同样的事情。我使用了System.DirectoryServices.ActiveDirectory 库,并使用了DomainDomainController 对象来查找您要查找的内容。

    这是我正在使用的代码:

    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);
            }
        }
    }
    

    然后,您只需调用DomainManager.DomainPath,一切都会初始化一次(它可以避免资源泄漏)或DomainName 等等。或者RootPath,对于为DirectorySearcher初始化根DirectoryEntry非常有用。

    我希望这能回答您的问题并有所帮助。

    【讨论】:

      【解决方案2】:

      是的,您正在寻找的是默认命名上下文,该信息保存在所有域通用的 RootDSE 上下文中:

      DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
      
      string defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value;
      

      【讨论】:

      • @Gnial0id Is there any way to get "LDAP://DC=ic,DC=local" dynamically? 我的回答怎么不能做到这一点?你甚至知道默认的命名上下文是什么吗?
      • 我的错,你是对的。对于造成的误解和情况,我深表歉意。我很遗憾你处理它的方式。
      【解决方案3】:

      您应该查看System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

      基本上,您可以定义域上下文并在 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....     
          }
      }
      

      新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

      【讨论】:

        猜你喜欢
        • 2015-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 2018-11-03
        • 2020-04-16
        相关资源
        最近更新 更多