【问题标题】:Find username from email address in active directory vb.net从活动目录 vb.net 中的电子邮件地址中查找用户名
【发布时间】:2018-05-03 05:10:15
【问题描述】:

抱歉,我查看了链接“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 并获取用户信息。

提前致谢

【问题讨论】:

  • 任何称职的 VB 开发人员都可以阅读 C# 代码,即使他们不会编写。还有很多在线代码转换器,至少可以让您获得很大的帮助。先做你能做的,如果它不起作用,然后再发布。那个C#代码包含一个using语句,和if语句和一个foreach语句,所有这些在VB中都有明显的等价物。如果您还没有编写包含这些代码的代码,那么您还没有尝试过,如果您还没有尝试过,那么在这里发布还为时过早。
  • 对于UserPrincipal 类型,就像它对所有其他类型所做的那样,文档会告诉您它在哪个程序集中声明以及它是哪个名称空间的成员。您是否引用并导入了这些?如果不是,那么 VS 当然不会识别该类型。
  • 您可能需要添加对System.DirectoryServices的 .NET 引用
  • 还有System.DirectoryServices.AccountManagement

标签: vb.net visual-studio-2010


【解决方案1】:

您需要将 .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 整理内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多