LDAP(轻量级目录访问协议)是一种用于访问和维护分布式目录信息服务的应用层协议。AD(Active Directory)是微软提供的一种目录服务,而LDS(Lightweight Directory Services)是Windows Server中的一个角色,提供了类似AD的功能,但更加轻量级。
以下是一个使用LdapConnection
连接到LDAP服务器的基本示例:
using System;
using System.DirectoryServices.Protocols;
public class LdapExample
{
public static void Main()
{
string ldapServer = "ldap.example.com";
string domain = "example.com";
string username = "user@example.com";
string password = "password";
try
{
using (LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(ldapServer)))
{
connection.AuthType = AuthType.Negotiate;
connection.Credential = new NetworkCredential(username, password, domain);
connection.Bind();
Console.WriteLine("Connection successful!");
}
}
catch (LdapException ldapEx)
{
Console.WriteLine($"LDAP error: {ldapEx.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"General error: {ex.Message}");
}
}
}
通过以上步骤和示例代码,你应该能够诊断并解决使用LdapConnection
连接到AD/LDS时遇到的凭据无效问题。
领取专属 10元无门槛券
手把手带您无忧上云