用C#读取LDAP记录

作者:管理员 更新时间:2014-03-29 11:37

private void  GetLDAPInfo()  

           {

              try

              {  

                 DirectoryEntry entry = new DirectoryEntry( "LDAP://ldap.xxx.com/o=xxx,c=an");  

                 entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;  

                 DirectorySearcher searcher = new DirectorySearcher(entry);  

                 searcher.Filter = "(alias=" + getLoginName() + ')';  

                 SearchResult result = searcher.FindOne();  

                 if (result == null)  

                 {  

                     LDAPInfo.Add("NotValid","yes");  

                     errnum = -40;  

                     return;  

                 }  


                 string path = result.Path;  

                 path = path.Substring(path.LastIndexOf("/") + 1);  

                 ResultPropertyCollection p = result.Properties;


                 string v = "";                  

                 if (p.Contains("edmworkstation"))  

                 {  

                     foreach (var a in p["edmworkstation"])  

                     {  

                         v += a.ToString().Substring(0, a.ToString().IndexOf('/')) + ;  

                      }  

                     LDAPInfo.Add("pcnames", v);  

                 }  

                else

                {  

                     LDAPInfo.Add("NotValid", "yes");  

                 }                  


                if (p.Contains("displayName"))  

                {  

                     v = "";  

                     foreach (var a in p["displayName"])

                    {  

                        v += a.ToString();  

                    }  

                    LDAPInfo.Add("displayname",v);      

                }  

            }  

            catch

           {  

                LDAPInfo.Add("NotValid", "yes");  

                errnum = -40;  

                return;  

            }  

         }  

来源:亦有软件
软件产品Eysln Software Product
亦有公告Eysln Notice
案例中心Eysln Template
知识库Eysln Knowledge Base
工具箱Eysln Toolkit Online