Feb 28

用C#读取LDAP记录 不指定

kcao , 17:05 , 技术 , 评论(0) , 引用(0) , 阅读(6529) , Via 本站原创

 

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

C# Code Copy Code To Clipboard
  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.         }  

 

Tags: , ,
Oct 1

C#搜索LDAP记录 不指定

kcao , 14:39 , 技术 , 评论(0) , 引用(0) , 阅读(2241) , Via 本站原创

以下代码可以把LDAP上指定ALIAS的人的所有信息查出来

C# Code Copy Code To Clipboard
  1. public void test(string username,string password)   
  2. {   
  3.     DirectoryEntry root = new DirectoryEntry("LDAP://ldap.slb.com/o=slb,c=an");   
  4.     root.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;   
  5.   
  6.     DirectorySearcher searcher = new DirectorySearcher(root);   
  7.     searcher.Filter = "(alias=" + Environment.UserName + ")";   
  8.     SearchResultCollection results=searcher.FindAll();   
  9.     SearchResult result=searcher.FindOne();   
  10.                    
  11.     PropertyCollection p = result.GetDirectoryEntry().Properties;   
  12.   
  13.     foreach (PropertyValueCollection i in p)   
  14.     {   
  15.         string v="";   
  16.         if (i.Count > 1)   
  17.         {   
  18.             foreach (string ii in i)   
  19.             {   
  20.                 v += ii + "\n";   
  21.             }   
  22.         }   
  23.         else  
  24.         {   
  25.             v = i.Value.ToString(); ;   
  26.         }   
  27.         MessageBox.Show(i.PropertyName + ": " + v);   
  28.     }  
Tags: , , ,
Sep 17

C#开发LDAP认证 不指定

kcao , 14:33 , 技术 , 评论(0) , 引用(0) , 阅读(2979) , Via 本站原创

开始时写的:

C# Code Copy Code To Clipboard
  1. DirectoryEntry root = new DirectoryEntry("ldap://ldap.slb.com:389",username,password);
  2. root.AuthenticationType = AuthenticationTypes.None;
  3. DirectorySearcher searcher = new DirectorySearcher(root);
  4. SearchResultCollection results=searcher.FindAll();

结果总是在FindAll()的地方出错,报告"Unknown Error 0x80005000",后来发现,协议的ldap一定要改成大写才行:

C# Code Copy Code To Clipboard
  1. DirectoryEntry root = new DirectoryEntry("LDAP://ldap.slb.com:389",username,password);
  2. root.AuthenticationType = AuthenticationTypes.None;
  3. DirectorySearcher searcher = new DirectorySearcher(root);
  4. SearchResultCollection results=searcher.FindAll();

这样写就对了,呵呵

Tags: ,
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]