我試圖連接到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;
}
}
我不認爲你應該在你的域名反斜槓 - 試試這個:'新PrincipalContext(ContextType.Domain, 「MyDomain.local」,空, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer))' – 2014-09-21 07:49:46
我在它不起作用之前試過了。謝謝,我找到解決方案。 – Anwar 2014-09-21 07:59:13