2023年6月21日发(作者:)

LDAP操作的两种⽅案最近由于项⽬需要研究了⼀下LDAP相关知识,感觉对没接触过的⼈来说还是有点坑的,所以记录下来给⼤家分享。由于是第⼀次接触,就在⽹上搜了⼀些相关的⽂章,照着⽰例代码测试,却怎么也连不上LDAP服务器,最后折腾的能连上服务器了,⼜不能检索⽤户。折腾过程中遇到的主要错误就是:There is no such object on the username or password is server could not be contacted.在经历了N⼩时的煎熬之后,终于找到了第⼀种解决⽅案,其实就是参考⽹上的⽰例代码,但是⽰例代码的AuthenticationTypes是None,测试连接的时候总是不能正常连接,LDAP地址只能写host,后⾯不能跟DN,否则就连不上服务器,⽽且这种⽅法连接上服务器也不能检索⽤户。后来改为nd之后才能正常⼯作了。 1 //---------------------------------------------------------------------------------------------- 2 // DirectoryEntry ⽅案, 需要引⽤ oryServices 3 //---------------------------------------------------------------------------------------------- 4 var ldapPath = "LDAP://" + host + "/" + baseDN; // LDAP必须要⼤写,好像是.NET的特⾊ 5 DirectoryEntry de = new DirectoryEntry(ldapPath, adminName, adminPass, nd); 6 DirectorySearcher searcher = new DirectorySearcher(de); 7 = "(uid=" + testUser + ")"; 8 Scope = e; 9 ("uid");10 ("cn");11

12 var result = e();13

14 // 输出⼏个查询的属性值15 foreach (string n in tyNames)16 {17 ine("{0}: {1}", n, ties[n][0].ToString());18 }19

20 try21 {22 int pos = dexOf('/');23 string uid = (0, pos + 1);24

25 // ⼆次连接,使⽤需要认证的⽤户密码尝试连接26 DirectoryEntry deUser = new DirectoryEntry(ldapPath, uid, testPass, nd);27 var connected = Object;28

29 ine("### 认证成功!");30 }31 catch32 {33 ine("认证失败~~~");34 }

另外⼀种⽅案是我同事找到的,和我上⾯⼀种⽅案⼏乎在同⼀时间找到,⽐较坑,是使⽤.NET官⽅类库中的LdapConnection,我⼀直认为LDAP这么常见的东西⼀定有官⽅的解决⽅案,奈何搜遍了国内外的中⽂、E⽂⽹站,“LDAP C#”、“LDAP .NET”关键字都搜了,就是没有任何⼈提到关于这个类的⽚⾔只字,真⽆语!难道这玩意就这么冷门吗?难道⼤家都在⽤DirectoryEntry吗?不可思议。 1 //------------------------------------------------------------------------------------------ 2 // LdapConnection ⽅案, 需要引⽤ ols 3 //------------------------------------------------------------------------------------------ 4 var identifier = new LdapDirectoryIdentifier(host); 5 var conn = new LdapConnection(identifier, new NetworkCredential 6 { 7 UserName = adminName, 8 Password = adminPass 9 });10 pe = ;11 ();12

13 var request = new SearchRequest(baseDN, "(uid=" + testUser + ")", e, "otherPassword");14 SearchResponse response = quest(request) as SearchResponse;15 if (s != null && > 0)16 {17 try18 {19 var connUser = new LdapConnection(identifier, new NetworkCredential20 {21 UserName = s[0].DistinguishedName,22 Password = testPass23 });24 pe = ;25 ();26

27 ine("### 认证成功!");28 }29 catch30 {31 ine("认证失败~~~ error password");32 }33 }34 else35 {36 ine("认证失败~~~ no user");37 }

测试代码中⽤到的⼀些变量声明:1 var host = ":389";2 var baseDN = "dc=xxx,dc=xxx,dc=com";3 var adminName = "uid=管理账号,ou=管理组," + baseDN;4 var adminPass = "管理密码";5 var testUser = "测试认证⽤户账号";6 var testPass = "测试认证⽤户密码";

2023年6月21日发(作者:)

LDAP操作的两种⽅案最近由于项⽬需要研究了⼀下LDAP相关知识,感觉对没接触过的⼈来说还是有点坑的,所以记录下来给⼤家分享。由于是第⼀次接触,就在⽹上搜了⼀些相关的⽂章,照着⽰例代码测试,却怎么也连不上LDAP服务器,最后折腾的能连上服务器了,⼜不能检索⽤户。折腾过程中遇到的主要错误就是:There is no such object on the username or password is server could not be contacted.在经历了N⼩时的煎熬之后,终于找到了第⼀种解决⽅案,其实就是参考⽹上的⽰例代码,但是⽰例代码的AuthenticationTypes是None,测试连接的时候总是不能正常连接,LDAP地址只能写host,后⾯不能跟DN,否则就连不上服务器,⽽且这种⽅法连接上服务器也不能检索⽤户。后来改为nd之后才能正常⼯作了。 1 //---------------------------------------------------------------------------------------------- 2 // DirectoryEntry ⽅案, 需要引⽤ oryServices 3 //---------------------------------------------------------------------------------------------- 4 var ldapPath = "LDAP://" + host + "/" + baseDN; // LDAP必须要⼤写,好像是.NET的特⾊ 5 DirectoryEntry de = new DirectoryEntry(ldapPath, adminName, adminPass, nd); 6 DirectorySearcher searcher = new DirectorySearcher(de); 7 = "(uid=" + testUser + ")"; 8 Scope = e; 9 ("uid");10 ("cn");11

12 var result = e();13

14 // 输出⼏个查询的属性值15 foreach (string n in tyNames)16 {17 ine("{0}: {1}", n, ties[n][0].ToString());18 }19

20 try21 {22 int pos = dexOf('/');23 string uid = (0, pos + 1);24

25 // ⼆次连接,使⽤需要认证的⽤户密码尝试连接26 DirectoryEntry deUser = new DirectoryEntry(ldapPath, uid, testPass, nd);27 var connected = Object;28

29 ine("### 认证成功!");30 }31 catch32 {33 ine("认证失败~~~");34 }

另外⼀种⽅案是我同事找到的,和我上⾯⼀种⽅案⼏乎在同⼀时间找到,⽐较坑,是使⽤.NET官⽅类库中的LdapConnection,我⼀直认为LDAP这么常见的东西⼀定有官⽅的解决⽅案,奈何搜遍了国内外的中⽂、E⽂⽹站,“LDAP C#”、“LDAP .NET”关键字都搜了,就是没有任何⼈提到关于这个类的⽚⾔只字,真⽆语!难道这玩意就这么冷门吗?难道⼤家都在⽤DirectoryEntry吗?不可思议。 1 //------------------------------------------------------------------------------------------ 2 // LdapConnection ⽅案, 需要引⽤ ols 3 //------------------------------------------------------------------------------------------ 4 var identifier = new LdapDirectoryIdentifier(host); 5 var conn = new LdapConnection(identifier, new NetworkCredential 6 { 7 UserName = adminName, 8 Password = adminPass 9 });10 pe = ;11 ();12

13 var request = new SearchRequest(baseDN, "(uid=" + testUser + ")", e, "otherPassword");14 SearchResponse response = quest(request) as SearchResponse;15 if (s != null && > 0)16 {17 try18 {19 var connUser = new LdapConnection(identifier, new NetworkCredential20 {21 UserName = s[0].DistinguishedName,22 Password = testPass23 });24 pe = ;25 ();26

27 ine("### 认证成功!");28 }29 catch30 {31 ine("认证失败~~~ error password");32 }33 }34 else35 {36 ine("认证失败~~~ no user");37 }

测试代码中⽤到的⼀些变量声明:1 var host = ":389";2 var baseDN = "dc=xxx,dc=xxx,dc=com";3 var adminName = "uid=管理账号,ou=管理组," + baseDN;4 var adminPass = "管理密码";5 var testUser = "测试认证⽤户账号";6 var testPass = "测试认证⽤户密码";