asp.net - 从 Active Directory 获取电子邮件地址

标签 asp.net vb.net webforms

全部--

我能够获取全名值 我正在尝试从 Active Directory 检索电子邮件地址,但在我使用 Windows 身份验证的 ASP.Net Web 窗体项目中使用以下代码:

Dim wi As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
Dim a As String() = wi.Name.Split(New Char() {"\"c}) '' Context.User.Identity.Name.Split('\')

Dim ADEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry(Convert.ToString("WinNT://" + a(0) + "/" + a(1)))
Dim Name As String = ADEntry.Properties("FullName").Value.ToString()
Dim Email As String = ADEntry.Properties("mail").Value.ToString()

当我到达尝试检索电子邮件地址的代码行时,我收到“未设置对象实例的对象引用”。错误。我试过使用电子邮件地址、电子邮件。我认为问题在于我只是使用了错误的关键字。我能够检索 FullName 值。

最佳答案

感谢Davide Piras谁发给我this link ,我找到了一个合适的解决方案:

Dim wi As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
Dim a As String() = wi.Name.Split(New Char() {"\"c}) '' Context.User.Identity.Name.Split('\')

Dim dc As PrincipalContext = New PrincipalContext(ContextType.Domain, "DomainName")
Dim adUser As UserPrincipal = UserPrincipal.FindByIdentity(dc, a(1))
Dim Email As String = adUser.EmailAddress

关于asp.net - 从 Active Directory 获取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913271/

相关文章:

vb.net - 为什么使用 TryCast 而不是 DirectCast?

c# - .NET 中的无效或意外参数应该抛出哪些异常?

asp.net - Sys.WebForms.Res 未定义

c# - 在 ASP .NET c# 中使用 JQuery 进行 Ajax

c# - 将值传递给 ASP.NET 中的 javascript

vb.net - 如何在VB.Net中将Array转换为ArrayList

c# - 以编程方式将样式应用于 asp.net 网站

c# - ASP.NET FormsAuthentication - 要解密的数据长度无效

c# - Google directions api 网络服务 - 在 map 上显示响应。是否可以?

c# - 如何缓存 XmlTextWriter 数据?