用C#读取LDAP记录

从LDAP读取记录,把edmworkstation和displayname这2项内容取出,放在一个dictionary中以备用.edmworkstation是用户的登记计算机名,可能含有多条记录.如果读取失败,则在dictionary中放一个"NotValid=yes"项目.


  1. private void GetLDAPInfo()   
  2.         {   
  3.             try  
  4.             {   
  5.                 DirectoryEntry entry = new DirectoryEntry("LDAP://ldap.xxx.com/o=xxx,c=an");   
  6.                 entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;   
  7.                 DirectorySearcher searcher = new DirectorySearcher(entry);   
  8.                 searcher.Filter = "(alias=" + getLoginName() + ')';   
  9.                 SearchResult result = searcher.FindOne();   
  10.                 if (result == null)   
  11.                 {   
  12.                     LDAPInfo.Add("NotValid""yes");   
  13.                     errnum = -40;   
  14.                     return;   
  15.                 }   
  16.   
  17.                 string path = result.Path;   
  18.                 path = path.Substring(path.LastIndexOf("/") + 1);   
  19.                 ResultPropertyCollection p = result.Properties;    
  20.   
  21.                 string v = "";                   
  22.                 if (p.Contains("edmworkstation"))   
  23.                 {   
  24.                     foreach (var a in p["edmworkstation"])   
  25.                     {   
  26.                         v += a.ToString().Substring(0, a.ToString().IndexOf('/')) + ";";   
  27.                     }   
  28.                     LDAPInfo.Add("pcnames", v);   
  29.                 }   
  30.                 else  
  31.                 {   
  32.                     LDAPInfo.Add("NotValid""yes");   
  33.                 }                   
  34.   
  35.                 if (p.Contains("displayName"))   
  36.                 {   
  37.                     v = "";   
  38.                     foreach (var a in p["displayName"])   
  39.                     {   
  40.                         v += a.ToString();   
  41.                     }   
  42.                     LDAPInfo.Add("displayname",v);      
  43.                 }   
  44.             }   
  45.             catch  
  46.             {   
  47.                 LDAPInfo.Add("NotValid""yes");   
  48.                 errnum = -40;   
  49.                 return;   
  50.             }   
  51.         }  
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值