Asp.net 如何提取Outlook显示的相同employeeID属性值?

Asp.net 如何提取Outlook显示的相同employeeID属性值?,asp.net,vb.net,active-directory,outlook,ldap,Asp.net,Vb.net,Active Directory,Outlook,Ldap,我们公司出于各种原因使用ActiveDirectory。其中之一是处理Outlook联系人和用户登录ID 我已经编写了一个程序来检测登录的用户id,并使用提取的登录id搜索Active Directory。然后,从Active Directory提取的信息存储在数据库中 下面是我用来提取ActiveDirectory信息数据的代码: Dim enTry As DirectoryEntry = _ New DirectoryEntry("LDAP://myCOMPANY/DC=myCOM

我们公司出于各种原因使用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值

我设计了下图来帮助你理解我正在经历的事情。 图像分为两个部分,每种情况一个部分

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

在案例1中,该员工已使用其ID登录到Windows:
xms33808

Outlook显示他的员工ID为
16078

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

ASP.Net命令窗口显示他的employeeID是
xms33808
,这不是真的

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

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

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

Outlook显示他的员工ID为
25163

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

ASP.Net命令窗口显示他的employeeID为
NOTHING

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

我的问题是:如何提取Outlook显示的相同employeeID值信息


如果您使用.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.....

有一个类似的广告属性叫“employeeNumber”,令人困惑。是否Outlook实际上正在使用此属性填充其显示

根据Outlook联系人卡上的Microsoft支持页面,“employeeID”不是您可以使用的字段。然而,“employeeNumber”是


希望这至少有助于推进您的故障排除工作。

最好的方法是连接到exchange server并查询所需的数据,然后从xml解析所需的信息。

我终于找到了保存员工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
旁边打印出的徽章编号

属性的非常奇怪的名称,应该称为
employeeID
staffID
BadgeNumber
或任何其他有意义的名称

后来,我修改了搜索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))"

非常奇怪-你没有辅助广告服务器,是吗?@DarkcatStudios没有。只有一个广告服务器。但是我怎样才能检查是否还有其他的呢?我的意思是,没有经过人为干预?正如Bo TX在下面提到的。。。您应该使用employeeNumber而不是employeeID,无论您选择哪种方式从AD查找详细信息。
objADUser。employeeNumber
导致代码编辑器生成错误,因为
employeeNumber
不是
objADUser
的有效属性或成员/属性。但是,
EmployeeId
是。我试图检查
resEnt.GetDirectoryEntry.Properties.Item(“employeeNumber”).Value的内容,但这两种情况都不适用。听起来您可能需要与Exchange管理员协调。他们应该能够确认将此字段拉入显示的位置和方式。如何查看来自AD服务器的XML响应?AD服务器不会提供u XML。XML是对Exchange服务器请求的响应。这样,它会变得更安全,而不是解析来自AD的数据。