错误:System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity中出现“操作错误”

问题描述 投票:6回答:3

我有以下代码来检索我的MVC3 Web应用程序中给定用户名的AD组:

PrincipalContext userDomain = new PrincipalContext(ContextType.Domain, username.Split('\\')[0]);
UserPrincipal user = UserPrincipal.FindByIdentity(userDomain, username);
PrincipalSearchResult<Principal> memberOfGroups = user.GetGroups();
IEnumerator<Principal> memberOfGroupsEnumerator = memberOfGroups.GetEnumerator();
List<string> userADGroups = new List<string>();

try
{
    while (memberOfGroupsEnumerator.MoveNext())
    {
        userADGroups.Add(memberOfGroupsEnumerator.Current.ToString());
    }
}
catch
{
    // When trying to access AD groups of a different domain, issues can arise at the end of the enumerator. These may be ignored.

}

这在本地工作正常,但当部署到网络上的另一台机器上时出错,出现以下错误:

发生了操作错误。

错误的堆栈跟踪:

System.DirectoryServices.DirectoryServicesCOMException(0x80072020):发生操作错误。 在System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 在System.DirectoryServices.DirectoryEntry.Bind() 在System.DirectoryServices.DirectoryEntry.get_AdsObject() 在System.DirectoryServices.PropertyValueCollection.PopulateList() 在System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry条目,String propertyName) 在System.DirectoryServices.PropertyCollection.get_Item(String propertyName) 在System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() 在System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() 在System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() 在System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context,Type principalType,Nullable`1 identityType,String identityValue,DateTime refDate) 在System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,String identityValue) 在MvcSFIWebSite.Models.User..ctor(String username)

错误消息是相当模糊的,我无法弄清楚发生了什么,因为它在本地工作正常。

用于部署的计算机上的IIS使用自定义帐户而不是AppPool标识。是否应授予此帐户访问AD组目录的任何权限? IIS中是否明确要求其他任何设置才能生效?

任何建议都会非常有帮助。提前致谢。

c# asp.net-mvc-3 iis-7
3个回答
17
投票

问题是因为在web.config中将identity_impersonate设置为true,因此传递的用户令牌是辅助令牌,因此无法访问Active Directory。

This answer解决了我的问题。


2
投票

我们也有这个问题,但配置文件没有这个设置。但在检查IIS中的各种选项后,我在UI中发现了类似的选项。

IIS Impersonation setting


0
投票

启用模拟并启用Windows身份验证后,Active Directory将不接受模拟用户的凭据。您可以使用基本身份验证而不是Windows身份验证来解决此问题。 PS。始终使用SSL进行基本身份验证

© www.soinside.com 2019 - 2024. All rights reserved.