连接到本地AD域控制器

时间:2013-09-23 03:33:04

标签: c#-4.0 active-directory ldap

我已经设置了一个带AD的开发服务器,我正试图弄清楚如何通过 .NET 连接到它。我正在安装AD的同一台机器上工作。我从AD获得了DC名称,以及机器的名称,但连接不起作用。我使用的是与用于连接服务器的凭据相同的凭据。

有什么建议吗?

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://[dc.computername.com]", "administrator", "[adminpwd]");

2 个答案:

答案 0 :(得分:0)

使用System.DirectoryServices.Protocols命名空间尝试这样的事情:

//Define your connection
LdapConnection ldapConnection = new LdapConnection("123.456.789.10:389");

try
{
      //Authenticate the username and password
      using (ldapConnection)
      {
          //Pass in the network creds, and the domain.
          var networkCredential = new NetworkCredential(Username, Password, Domain);
          //Since we're using unsecured port 389, set to false. If using port 636 over SSL, set this to true.
          ldapConnection.SessionOptions.SecureSocketLayer = false;
          ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
          //To force NTLM\Kerberos use AuthType.Negotiate, for non-TLS and unsecured, use AuthType.Basic
          ldapConnection.AuthType = AuthType.Basic;
          ldapConnection.Bind(networkCredential);
      }
      catch (LdapException ldapException)
      {
          //Authentication failed, exception will dictate why
      }
}

答案 1 :(得分:0)

您可以连接到RootDSE容器吗?

DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE", "administrator", "[adminpwd]");

如果可行,您可以读出存储在该根容器中的一些属性

if (rootDSE != null)
{
   Console.WriteLine("RootDSE Properties:\n\n");

   foreach (string propName in rootDSE.Properties.PropertyNames)
   {
       Console.WriteLine("{0:-20d}: {1}", propName, rootDSE.Properties[propName][0]);
   }
}

这将显示有关安装中存在哪些LDAP路径的一些信息。