zoukankan      html  css  js  c++  java
  • 从AD( Active Directory )读取 Email

    公司电脑使用AD进行管理,非Work Group管理。
    建立AD帐号了,才有”公司Email“,所以从AD获取”公司Email“是再好不过的了。


    了解 AD( Active Directory )
    Active Directory 是树形结构。树形结构中的每一个节点都包含一组属性。


    查看AD结构
    使用MS提供的工具,ADExplorer.exe 。


    .NET 对AD支持
    System.DirectoryServices 命名空间
    1 命名空间包含两个组件类别:DirectoryEntry 和 DirectorySearcher,它们使用 Active Directory Services Interface (ADSI) 技术。
    2 提供了对其添加、修改和删除。
    DirectoryEntry 类别
    1 封装 Active Directory 底层架构中的节点或对象。
    2 系结至对象、读取属性 (Property) 和更新属性 (Attribute)。
    3 提供生命周期管理与导航方法的支持,包括建立、删除、重新命名、移除子节点和列举子系等。
    DirectoryServices.DirectorySearcher
    1 执行对 Active Directory 底层架构的查询。
    2 传回 SearchResult 的实例,其包含在 SearchResultCollection 类别的实例中。
    DirectoryServices.SearchResult
    DirectoryServices.SearchResultCollection


    查询语法
    筛选条件使用下列方针:
    1 字串必须包含在括号内。
    3 表达式可以使用关系运算符:<、<=、=、>= 和 >。一个范例是 "(objectClass=user)"。另一个范例是 "(lastName>=Davis)"。
    3 复合表达式是以前置运算符 & 和 |组成的。一个范例是 "(&(objectClass=user)(lastName= Davis))"。
    另一个范例是 "(&(objectClass=printer)(|(building=42)(building=43)))"。
    详细:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/search_filter_syntax.asp)


    实际例子

    <add key="ADServerPath" value="LDAP://192.168.0.6/DC=wood,DC=com,DC=cn"/>
    /// <summary>
    /// 依据AD帐号从 AD 获取用户的email。
    /// </summary>
    /// <param name="memberID">AD帐号</param>
    /// <returns></returns>
    public static string GetMailAddressFromAD(string memberID)
    {
    	string mailAdd = string.Empty;
    	System.DirectoryServices.DirectorySearcher search = null;
    	System.DirectoryServices.SearchResult r = null;
    
    	try
    	{
    		search = new System.DirectoryServices.DirectorySearcher();
    		string ADServerPath = ConfigurationManager.AppSettings["ADServerPath"].ToString();
    		string filter =
    			"(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)
    
    (samAccountName=" + memberID + ")(|(homeMDB=*)(msExchHomeServerName=*))) ))))";
    			
    		search.SearchRoot = new System.DirectoryServices.DirectoryEntry(ADServerPath);
    		search.Filter = filter;
    		search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
    
    		r = search.FindOne();
    
    		if (r != null)
    		{
    			mailAdd = r.Properties["mail"][0].ToString();
    		}
    	}
    	catch (Exception ex)
    	{
    		throw new Exception("获取用户邮件地址时出错。",ex);
    	}
    	return mailAdd;
    }
    /// <summary>
    /// 依据用户名从 AD 获取用户的email。
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <returns></returns>
    public static string GetMailAddressFromADByUserName(string userName)
    {
    	string mailAdd = string.Empty;
    	System.DirectoryServices.DirectorySearcher search = null;
    	System.DirectoryServices.SearchResult r = null;
    
    	try
    	{
    		search = new System.DirectoryServices.DirectorySearcher();
    		string ADServerPath = ConfigurationManager.AppSettings["ADServerPath"].ToString();
    
    		search.SearchRoot = new System.DirectoryServices.DirectoryEntry(ADServerPath);
    		// * 表示通配符,类似SQL 的 Like '%userName%'
    		search.Filter = "(displayName=*" + userName + "*)";
    		search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
    
    		r = search.FindOne();
    
    		if (r != null)
    		{
    			mailAdd = r.Properties["mail"][0].ToString();
    		}
    	}
    	catch (Exception ex)
    	{
    		throw new Exception("获取用户邮件地址时出错。",ex);
    	}
    	return mailAdd;
    }
    人的一生应该这样度过:当他回首往事的时候,不会因为虚度年华而悔恨,也不会因为碌碌无为而羞愧。
  • 相关阅读:
    LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
    LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word
    LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
    LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
    LeetCode225 Implement Stack using Queues
    LeetCode150 Evaluate Reverse Polish Notation
    LeetCode125 Valid Palindrome
    LeetCode128 Longest Consecutive Sequence
    LeetCode124 Binary Tree Maximum Path Sum
    LeetCode123 Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/htht66/p/2367182.html
Copyright © 2011-2022 走看看