我尝试连接到Active Directory以获取当前用户的名称,但我收到了错误:
无法联系服务器。
代码:
var identityName = HttpContext.Current.User.Identity.Name;
using (HostingEnvironment.Impersonate())
{
using (var context = new PrincipalContext(ContextType.Domain, "\\MyDomain.local", null, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer))
using (userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, identityName))
{
lblUserName.Text = userPrincipal.DisplayName;
}
}
答案 0 :(得分:0)
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
string[] a = Context.User.Identity.Name.Split('\\');
System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
lblUserName.Text = ADEntry.Properties["FullName"].Value.ToString();