通过电子邮件从LDAP获取sAMAccountName

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过电子邮件从LDAP获取sAMAccountName相关的知识,希望对你有一定的参考价值。

Uses LDAP to find sAMAccountName by using the Email address.

Add System.DirectoryServices to references.
  1. public static string GetSAMAccountNameByEmail(string email)
  2. {
  3. var oroot = new System.DirectoryServices.DirectoryEntry("LDAP://INVESTOR_AB");
  4. var osearcher = new System.DirectoryServices.DirectorySearcher(oroot);
  5. osearcher.Filter = string.Format("(&(mail={0}))", email);
  6. var oresult = osearcher.FindAll();
  7.  
  8. if (oresult.Count == 0) throw new InvalidOperationException(string.Format("Cannot find mail {0} in LDAP.", email));
  9. if (oresult.Count > 1) throw new InvalidOperationException(string.Format("There are {0} items with mail {1} in LDAP.", oresult.Count, email));
  10.  
  11. return oresult[0].Properties["sAMAccountName"][0].ToString();
  12. }

以上是关于通过电子邮件从LDAP获取sAMAccountName的主要内容,如果未能解决你的问题,请参考以下文章