错误连接到Active Directory

问题描述:

我试图连接到Active Directory来获取当前用户的名称,但我得到一个错误,而不是:错误连接到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

我不认为你应该在你的域名反斜杠 - 试试这个:'新PrincipalContext(ContextType.Domain, “MyDomain.local”,空, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer))' – 2014-09-21 07:49:46

+0

我在它不起作用之前试过了。谢谢,我找到解决方案。 – Anwar 2014-09-21 07:59:13

  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(); 
+0

但是'WinNT://'提供程序正在使用**机器本地**帐户 - ***不*** ***活动目录帐户.... – 2014-09-21 08:53:10

+0

是的,就像我说的当我发布web应用程序到iis它的显示我错误“ndex超出了数组的范围。”但在当地它的工作:( – Anwar 2014-09-21 09:07:17