C#提供了 LdapConnection 类用于连接Microsoft Active Directory 域服务或 LDAP 服务器的 TCP/IP 或 UDP LDAP 连接。
下面是连接 Ldap的连接方法和大家分享下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1
staticLdapConnectionldapConnection;
2
staticstringldapServer;
3
staticNetworkCredentialcredential;
4
staticstringtargetOU;
5
staticstringpwd;
6
publicvoidLdapBind()
7

{
8
ldapServer="172.18.69.204:389";
9
targetOU="cn=Manager,dc=tst,dc=com";
10
pwd="000000";
11
12
//credential=newNetworkCredential(String.Empty,String.Empty);
13
credential=newNetworkCredential(targetOU,pwd);
14
15
16
stringdn="";
17
18
//ldapConnection=newLdapConnection(newLdapDirectoryIdentifier(ldapServer));
19
//ldapConnection.SessionOptions.ProtocolVersion=3;//Ldap协议版本
20
//ldapConnection.AuthType=AuthType.Anonymous;//不传递密码进行连接
21
22
ldapConnection=newLdapConnection(ldapServer);
23
ldapConnection.AuthType=AuthType.Basic;
24
ldapConnection.Credential=credential;
25
26
try
27

{
28
Console.WriteLine("链接
.");
29
ldapConnection.Bind();
30
Console.WriteLine("链接成功");
31
32
}
33
catch(Exceptionee)
34

{
35
Console.WriteLine(ee.Message);
36
}
37
38
39
ldapConnection.Dispose();
40
41
}

2

3

4

5

6

7



8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27



28


29

30

31

32

33

34



35

36

37

38

39

40

41

注意
1、如果我们使用ldapConnection.AuthType = AuthType.Anonymous; 的认证方式,就一定要让Dn与Pwd为空,实现匿名认证方式,如:
credential = new NetworkCredential(String.Empty, String.Empty);
2、使用c#连接Ldap服务器,还可以使用 Novell公司的Novell.Directory.Ldap来实现。