2013-12-09 45 views
10

由于各种原因,我们公司使用ActiveDirectory。其中之一是处理Outlook联系人和用户登录ID。如何提取Outlook正在显示的相同employeeID属性值?

我写了一个程序来检测登录的用户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 

出于某种原因,一些用户没有值的雇员ID字段,以及一些有。

但是,所有用户将在Outlook中浏览时显示employeeID值。

我设计了以下图像以帮助您了解我正在经历什么。 该图像分为两个部分,每个CASE部分。

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

在案例1,员工使用自己的ID已经登录到Windows:xms33808

展望表明,他的员工ID为16078

展望显示他的电子邮件别名是xms33808

ASP.Net命令窗口中显示,他的员工ID为xms33808,这是不正确

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

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

在案例2中,员工使用ID已经登录到Windows:25163

展望表明,他的雇员ID 25163

Outlook显示他的电子邮件别名是MutawaAAB

ASP.Net命令窗口显示他的员工ID是NOTHING

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

我的问题是:我怎样才能提取相同的雇员价值信息Outlook正在显示?

Case 1: employeeID is different from ASP.Net than Outlook Case 2: employeeID is found in Outlook, but Nothing in ASP.Net

+0

很奇怪 - 你没有辅助AD服务器吗? –

+0

@DarkcatStudios号只有一个AD服务器。但是我怎样才能检查是否有另一个?我的意思是没有通过IT人为干预? – Ahmad

回答

0

我终于找到持有该员工ID属性。

对于那些寻找一个答案,同样的问题,这里是属性的名称:

extensionattribute2

我碰到这个来的方式是通过打印出所有属性的搜索条目的价值观结果..

For Each resEnt In mySearcher.FindAll() 
    Debug.Print("The properties of the " + resEnt.GetDirectoryEntry().Name + " are :") 
     For Each singleAttribute As String In DirectCast(resEnt.Properties, ResultPropertyCollection).PropertyNames 
      Debug.Print(singleAttribute & Convert.ToString(" = ")) 
      For Each singleValue As [Object] In DirectCast(resEnt.Properties, ResultPropertyCollection)(singleAttribute) 
       Debug.Print(vbTab + singleValue.ToString) 
      Next 
     Next 
Next 

然后我检查了输出,发现徽章号打印出旁边extensionattribute2 ..

很奇怪的名字是一个应该被称为employeeIDstaffIDBadgeNumber或其他任何有意义的财产。

之后,我修改了我的代码,该代码搜索Active Directory时不仅要通过Sama账户进行搜索,还要通过此extensionattribute2事件进行搜索,因为每个员工都有唯一的ID。

Dim LookFor As String = "25163" 
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry) 

mySearcher.Filter = "(&(| (anr=" & LookFor & ")" & _ 
         "(extensionattribute2=" & LookFor & "))" & _ 
         "(objectCategory=Person)(objectClass=user))" 
1

如果你使用.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..... 
+0

正如Bo TX在下面提到的,您应该使用employeeNumber而不是employeeID,无论您选择哪种方式来从AD中查找详细信息。 – Mych

+1

'objADUser.EmployeeNumber'导致代码编辑器生成错误,因为'EmployeeNumber'不是'objADUser'的有效属性或成员/属性。然而,'EmployeeId'是。 – Ahmad

+0

艾哈迈德....你是对的...我不知道我在哪里,从 – Mych

1

有一个称为 'employeeNumber' 一个混淆的相似AD属性。难道是Outlook实际上使用这个属性来填充它的显示?

每对Outlook联系人卡片此Microsoft支持页面上,“雇员”不在于你可以使用一个字段。但是,“员工编号”是。

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

希望这有助于至少提前进行故障诊断处理。

+0

得到EmployeeNumber我试图检查'resEnt.GetDirectoryEntry.Properties.Item(“employeeNumber”)的内容。价值“,但对于这两种情况都不是什么问题 – Ahmad

+0

听起来您可能需要与Exchange管理员协调。他们应该能够确认该字段在哪里以及如何被拉出来显示。 –

0

最好的办法是连接到Exchange Server和查询所需数据,然后解析从XML所需的信息。

+0

如何查看AD服务器的XML响应? – Ahmad

+0

AD服务器不会提供xml。xml是对Exchange Server发出的请求的响应。这样,它就变得更安全了,而不是解析AD中的数据。 –

相关问题