如何使用LdapConnection 类 (1)链接 Ldap服务器

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 static LdapConnectionldapConnection;
2 static string ldapServer;
3 static NetworkCredentialcredential;
4 static string targetOU;
5 static string pwd;
6 public void LdapBind()
7 {
8ldapServer="172.18.69.204:389";
9targetOU="cn=Manager,dc=tst,dc=com";
10pwd="000000";
11
12//credential=newNetworkCredential(String.Empty,String.Empty);
13credential=newNetworkCredential(targetOU,pwd);
14
15
16stringdn="";
17
18//ldapConnection=newLdapConnection(newLdapDirectoryIdentifier(ldapServer));
19//ldapConnection.SessionOptions.ProtocolVersion=3;//Ldap协议版本
20//ldapConnection.AuthType=AuthType.Anonymous;//不传递密码进行连接
21
22ldapConnection=newLdapConnection(ldapServer);
23ldapConnection.AuthType=AuthType.Basic;
24ldapConnection.Credential=credential;
25
26try
27{
28Console.WriteLine("链接.");
29ldapConnection.Bind();
30Console.WriteLine("链接成功");
31
32}

33catch(Exceptionee)
34{
35Console.WriteLine(ee.Message);
36}

37
38
39ldapConnection.Dispose();
40
41}

注意

1、如果我们使用ldapConnection.AuthType = AuthType.Anonymous; 的认证方式,就一定要让Dn与Pwd为空,实现匿名认证方式,如:

credential = new NetworkCredential(String.Empty, String.Empty);

2、使用c#连接Ldap服务器,还可以使用 Novell公司的Novell.Directory.Ldap来实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值