【问题标题】:Error: "An operations error occurred" in System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity错误:System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity 中出现“操作错误”
【发布时间】:2014-02-27 01:24:36
【问题描述】:

我有以下代码在我的 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(布尔 throwIfFail)
在 System.DirectoryServices.DirectoryEntry.Bind()
在 System.DirectoryServices.DirectoryEntry.get_AdsObject()
在 System.DirectoryServices.PropertyValueCollection.PopulateList()
在 System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry 条目,字符串属性名称)
在 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()
在 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext 上下文,类型 principalType,Nullable`1 identityType,字符串 identityValue,DateTime refDate)
在 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext 上下文,字符串 identityValue)
在 MvcSFIWebSite.Models.User..ctor(字符串用户名)

错误消息相当模糊,我无法弄清楚发生了什么,因为它在本地运行良好。

用于部署的计算机上的 IIS 使用自定义帐户而不是 AppPool 身份。是否应该授予此帐户访问 AD 组目录的任何权限? IIS 中是否需要任何其他设置才能使其正常工作?

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

【问题讨论】:

    标签: c# asp.net-mvc-3 iis-7


    【解决方案1】:

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

    This answer 解决了我的问题。

    【讨论】:

    • 我将 AppPool 身份(高级设置)更改为“NetworkService”,它解决了我的问题。
    • 所以基本上:所有活动目录语句都在: using (HostingEnvironment.Impersonate()) { ... }
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      启用模拟并启用 Windows 身份验证后,Active Directory 将不接受被模拟用户的凭据。 您可以通过使用 Basic Authentication 而不是 windows 身份验证来解决这个问题。 PS。始终将 SSL 与基本身份验证一起使用

      【讨论】:

        猜你喜欢
        • 2019-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 2021-04-22
        • 2016-06-06
        相关资源
        最近更新 更多