从活动目录vb.net中的电子邮件地址中查找用户名

时间:2017-11-20 02:30:19

标签: vb.net visual-studio-2010

抱歉,我查看了链接“Find username from Active Directory using email id”,但这是针对C#我无法弄清楚如何在Vb.net中进行操作。

在我的gridview中,当我选择要获取电子邮件ID的行并将其传递给AD以查找用户名时,但到目前为止我无法确定哪个命令会在VB.net中提供该详细信息

Protected Sub grdValidate_RowUpdating(sender As Object, e As EventArgs)
    Dim strEmail As String = grdValidate.SelectedRow.Cells(2).Text
    Dim ctx As New PrincipalContext(ContextType.Domain)

    ' find a user
    Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, strEmail)

End Sub

我看到这个属性“UserPrincipal.EmailAddress”,但VS甚至不识别该命令。显然我导入了

Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement

我正在尝试找到一个命令来传递电子邮件并匹配AD中的电子邮件ID并获取用户信息。

先谢谢

1 个答案:

答案 0 :(得分:0)

您需要将.NET引用添加到System.DirectoryServicesSystem.DirectoryServices.AccountManagement,然后再添加......

Using context As New System.DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Domain, strDomainName)
  Dim yourUser As System.DirectoryServices.AccountManagement.UserPrincipal = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(context, strEmailAddress)
  If yourUser IsNot Nothing Then
    strFirstName = yourUser.GivenName
    strLastName = yourUser.Surname
  End If
End Using
MsgBox(strFirstName & " " & strLastName)

为了清晰起见,我使用了完全限定名称,但您可以在模块开头使用Imports System.DirectoryServices.AccountManagement整理一下