![](/img/trans.png)
[英]Using DirectoryServices.AccountManagement, how do I get the e-mail address of an active directory security group?
[英]How to get a user's e-mail address from Active Directory?
我試圖在 AD 中獲取用戶的電子郵件地址但沒有成功。
String account = userAccount.Replace(@"Domain\", "");
DirectoryEntry entry = new DirectoryEntry();
try {
DirectorySearcher search = new DirectorySearcher(entry);
search.PropertiesToLoad.Add("mail"); // e-mail addressead
SearchResult result = search.FindOne();
if (result != null) {
return result.Properties["mail"][0].ToString();
} else {
return "Unknown User";
}
} catch (Exception ex) {
return ex.Message;
}
任何人都可以看到問題或指出正確的方向嗎?
免責聲明:此代碼不會搜索單個完全匹配項,因此對於domain\\j_doe
,如果存在類似名稱的帳戶,它可能會返回domain\\j_doe_from_external_department
的電子郵件地址。 如果這種行為是不可取的,那么要么使用samAccountName過濾器而不是下面使用的anr過濾器,要么另外過濾結果。
我已成功使用此代碼(其中“帳戶”是沒有域(域\\帳戶)的用戶登錄名:
// get a DirectorySearcher object
DirectorySearcher search = new DirectorySearcher(entry);
// specify the search filter
search.Filter = "(&(objectClass=user)(anr=" + account + "))";
// specify which property values to return in the search
search.PropertiesToLoad.Add("givenName"); // first name
search.PropertiesToLoad.Add("sn"); // last name
search.PropertiesToLoad.Add("mail"); // smtp mail address
// perform the search
SearchResult result = search.FindOne();
你們太辛苦了:
// Look up the current user's email address
string eMail = UserPrincipal.Current.EmailAddress;
您可以嘗試下面的 GetUserEmail 方法。 如果您想在 MVC 中查找已登錄用戶的電子郵件地址,請使用User.Identity.Name調用GetUserEmail()函數
using System.DirectoryServices;
using System.Linq;
public string GetUserEmail(string UserId)
{
var searcher = new DirectorySearcher("LDAP://" + UserId.Split('\\').First().ToLower())
{
Filter = "(&(ObjectClass=person)(sAMAccountName=" + UserId.Split('\\').Last().ToLower() + "))"
};
var result = searcher.FindOne();
if (result == null)
return string.Empty;
return result.Properties["mail"][0].ToString();
}
GetUserEmail(User.Identity.Name) //Get Logged in user email address
你忘記了過濾器。
在調用 FindOne 之前嘗試添加:
search.Filter = String.Format("(sAMAccountName={0})", account);
您需要為 System.DirectoryServices.AccountManagement 添加引用,並在 using 語句中包含相同的引用。 現在您可以訪問當前用戶的登錄詳細信息,如下所列,包括電子郵件地址。
string loginname = Environment.UserName;
string firstname = UserPrincipal.Current.GivenName;
string lastname = UserPrincipal.Current.Surname;
string name = UserPrincipal.Current.Name;
string eMail = UserPrincipal.Current.EmailAddress;
那這個呢
public string GetEmailFromSamAccountName(string samAccountName, string domain="YOURCOMPANY")
{
using (var principalContext = new PrincipalContext(ContextType.Domain, domain))
{
var userPrincipal = UserPrincipal.FindByIdentity(principalContext, samAccountName);
return userPrincipal.EmailAddress;
}
}
另外,您從哪里提取用戶名(存儲的、用戶輸入的、當前身份)? 用戶名可以輕松更改(重命名)-另一方面,SID/Windows 登錄標識不會更改-因此,如果可能和/或設計上需要,您最好按 SID 而不是 samaccountname 進行過濾/搜索。 ..
更新:弗雷德里克釘了它....
雅各布是對的。 您需要過濾您的搜索。 如果需要,您也可以在那里執行各種and
s 和or
s,但我認為sAMAccountName
就足夠了。 您可能想要啟動 ADSI 工具(我認為它在資源工具包中),它可以讓您像注冊注冊表一樣處理 AD。 非常適合查看屬性。 然后找到一個用戶,找出你想要的道具(在這種情況下是郵件)以及它的primary key
是什么 - sAMAccountName
是一個很好的,但你可能還想過濾節點類型。
我使用的是 mac,所以我無法為您檢查,但是 AD 中的每個節點都有一個類型,您可以將其添加到您的過濾器中。 我認為它看起來像這樣:
((sAMAccountName=bob) & (type=User))
再次檢查 - 我知道它不是 type=user,而是類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.