全部 -
我能够retieve我试图从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()
当我到达的代码行,我尝试检索电子邮件地址,我得到一个“对象引用不设置到对象的实例。” 错误。 我一直在使用EmailAddress的,电子邮件尝试。 我认为问题是,我只是使用了错误的关键字。 我能够检索全名值。
由于达维德Piras酒店谁给我这个链接 ,我找到了一个合适的解决方案:
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
此代码对我的作品..参考System.DirectoryServices.AccountManagement
static string GetADUserEmailAddress(string username)
{
using (var pctx = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username))
{
return up != null && !String.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty;
}
}
}
使用方法:
lblEmail.Text = GetADUserEmailAddress(Request.ServerVariables["AUTH_USER"]);