Asp.net

如何提取與 Outlook 顯示的相同的 employeeID 屬性值?

  • April 21, 2015

我們公司出於各種原因使用 ActiveDirectory。其中之一是處理 Outlook 聯繫人和使用者登錄 ID。

我編寫了一個程序來檢測登錄的使用者 ID,並使用提取的登錄 ID 搜尋 Active Directory。然後從 Active Directory 中提取的資訊儲存在數據庫中。

這是我用來提取 ActiveDirectory 資訊數據的程式碼:

Dim enTry As DirectoryEntry = _
    New DirectoryEntry("LDAP://myCOMPANY/DC=myCOMPANY,DC=myCOMPANY,DC=com")

Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = "(&(objectClass=user)(anr=" & thisUser & "))" 
'thisUser is the variable holding the Windows ID that is accessing the ASPX page


mySearcher.PropertiesToLoad.Add("employeeID")   'just in case I need to do this.

Dim resEnt As SearchResult

Try
 For Each resEnt In mySearcher.FindAll()

 Dim fullname As String = resEnt.GetDirectoryEntry.Properties.Item("cn").Value
 'fullname will always pull the right information

 Dim e_id As String = resEnt.GetDirectoryEntry.Properties.Item("employeeID").Value
 'e_id will sometimes be NOTHING, sometimes will contain an ID that
 '   is different from the one displayed in Outlook Contact Information
 '   and sometimes it will be matching the employeeID listed in Outlook info

Catch ex as Exception
 Log("Failed to pull AD data: " & ex.Message)
End Try

出於某種原因,有些使用者的employeeID 欄位沒有值,有些則有。

但是,所有使用者在 Outlook 中瀏覽時都將顯示一個employeeID 值。

我設計了以下圖片來幫助您了解我正在經歷的事情。圖像分為兩個部分,每個 CASE 一個部分。

========================================================

在案例 1中,員工使用他的 ID 登錄到 Windows:xms33808

Outlook 顯示他的員工 ID 是16078

Outlook 顯示他的電子郵件別名是xms33808

ASP.Net 命令視窗顯示他的employeeID 是xms33808,這是不正確的

======================================================

=======================================================

在案例 2中,員工使用 ID 登錄到 Windows:25163

Outlook 顯示他的員工 ID 是25163

Outlook 顯示他的電子郵件別名是MutawaAAB

ASP.Net 命令視窗顯示他的employeeID 是NOTHING.

=======================================================

我的問題是:如何提取與 Outlook 顯示的相同的employeeID 值資訊?

案例 1:employeeID 不同於 ASP.Net 而不是 Outlook 案例 2:在 Outlook 中找到了employeeID,但在 ASP.Net 中沒有找到

有一個令人困惑的類似 AD 屬性,稱為“employeeNumber”。難道 Outlook 實際上是在使用這個屬性來填充它的顯示?

根據 Outlook 聯繫人卡片上的 Microsoft 支持頁面,“employeeID”不是您可以使用的欄位。但是,’employeeNumber’ 是。

http://support.microsoft.com/kb/981022

希望這至少有助於推進您的故障排除工作。

如果您使用 .net 3.5 或更高版本,您可以使用以下比 LDAP 等更容易的…

System.DirectoryServices.AccountManagement

要獲取特定使用者的詳細資訊:

Dim objPC As PrincipalContext = Nothing 
Dim objADUser As UserPrincipal = Nothing
Try 
   objPC = New PrincipalContext(ContextType.Domain, "YourDomain")
   objADUser = UserPrincipal.FindByIdentity(objPC, "NetworkLogin") 
Catch ex As Exception 
End Try

獲取目前登錄使用者的詳細資訊

Dim objADUser As UserPrincipal = Nothing
Try
   objADUser = UserPrincipal.Current
Catch ex As Exception
End Try

然後,您可以詢問對象 objADUser 並獲取各種詳細資訊,例如

objADUser.VoiceTelephoneNumber
objADUser.EmailAddress
objADUser.EmployeeNumber and many others.....

引用自:https://stackoverflow.com/questions/20472109