zoukankan      html  css  js  c++  java
  • novell.directory.ldap获取邮箱活动目录

    在windows系统上可以使用下列方法来查找所有的员工邮箱和员工组:

     1 StringDictionary ReturnArray = new StringDictionary();
     2                 Dictionary<string, string> resultDict = new Dictionary<string, string>();
     3                 DirectoryEntry deDirEntry = new DirectoryEntry("LDAP://mail.test.com",
     4                                                                    UserName,
     5                                                                    Password,
     6                                                                    AuthenticationTypes.Secure);
     7 
     8                 
     9 
    10                 DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
    11                 
    12                 string sFilter = String.Format("(&(mailnickname=*)(|(objectcategory=user)(objectcategory=group)))");//(objectcategory=user)(objectcategory=group)
    13 
    14                 mySearcher.Filter = sFilter;
    15                 mySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
    16                 mySearcher.Sort.PropertyName = "cn";
    17                 mySearcher.PageSize = 1000;
    18 
    19                 SearchResultCollection results;
    20                 results = mySearcher.FindAll();
    View Code

    但是在移动端使用xamarin开发的时候是不能用上面的类的,需要使用其他的类库。

    下面推荐使用novell.directory.ldap类库,可以在NuGet上搜索LDAP即可查到,在我们安装程序包时可能遇到在PCL(可移植的)上无法安装,没关系,我们可以单独只装各平台的代码就可以了,虽然写的代码一样,但是就是要分平台来写,下面是例子:

     1 cn.Connect("mail.test.com", 389);
     2                 cn.Bind("test@test.com", "**password**");
     3                 string sFilter = String.Format("(&(mailnickname=*)(|(objectcategory=user)(objectcategory=group)))");
     4                 LdapSearchResults ldapSearchResults = cn.Search("CN=Users,DC=corp,DC=test,DC=com", LdapConnection.SCOPE_SUB, sFilter, null, false);
     5                 var entries = new List<LdapEntry>();
     6                 try
     7                 {
     8                     while (ldapSearchResults.hasMore())
     9                     {
    10                         entries.Add(ldapSearchResults.next());
    11                     }
    12                 }
    13                 catch (LdapException ex)
    14                 {
    15                     System.Console.WriteLine(ex.LdapErrorMessage);
    16                 }
    View Code

    注意事项,使用novell.directory.ldap时,有部分帐号无法连接。Search的参数请根据实际情况配置。

  • 相关阅读:
    网站架构探索(3)负载均衡的方式
    架构师之路(6)OOD的开闭原则
    也谈IT人员流失问题 王泽宾
    技术体系的选择之Java篇
    网站架构探索(2)CDN基本常识
    设计模式之单例模式
    网站架构探索(1)序言 王泽宾
    架构师之路(39)IoC框架
    发展之道:简单与专注
    修me30打印机
  • 原文地址:https://www.cnblogs.com/zuimengaitianya/p/7059949.html
Copyright © 2011-2022 走看看