活动目录COMexception – 发生操作错误(0x80072020)

我得到一个间歇性COMexception“ 操作错误发生(0x80072020) ”(如下所示)当我尝试使用方法GroupPrincipal.FindByIdentity查询Active Directory

这是我的代码:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName); GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "Group to find"); 

我收到exception:

 Inner Exception: System.Runtime.InteropServices.COMException (0x80072020): An operations error occurred. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.PropertyValueCollection.PopulateList() at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) at System.DirectoryServices.PropertyCollection.get_Item(String propertyName) at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) at System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) 

代码从Windows 2003 SP2服务器上的Windows 服务运行。

我发现了另一个堆栈溢出的问题, 活动目录,列举用户的组,COMexception ,build议启用Kerberos作为PrincipalContext构造函数中的一个选项将解决这个问题,但我收到了不同于这个问题的hex代码

我的问题是

  1. 这个特殊的COMexception肯定是一个身份validation问题? 我需要确保在发布软件之前,这将100%解决问题。
  2. 是否有资源列出所有可能的COMexceptionhex代码,以便将来可以帮助自己更好一点?

问题往往在于Active Directory调用的上下文是在没有权限的用户之下(也可能在ASP.NET中的identity impersonate="true"时发生,这是因为用户令牌是从以下位置对另一台服务器进行身份validation时无法使用的“辅助令牌”: https : //social.technet.microsoft.com/Forums/en-US/f188029c-51cf​​-4b50-966a-eee7160d0353/an-operations-error-occured )。

以下代码将确保您正在运行的代码块在运行您的服务或站点的AppPool (即NETWORKSERVICE )环境下运行。

 using (HostingEnvironment.Impersonate()) { var domainContext = new PrincipalContext(ContextType.Domain, "myDomain.com"); var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.Name, "PowerUsers"); if (groupPrincipal != null) { //code to get the infomation } } 

但是,一个非常重要的细节是, 所有调用Active Directory的代码都必须位于该块中。 我曾经使用过我的一个团队成员写过的一些代码,它返回了一个LINQ查询结果的Userstypes(自定义类),但没有评估expression式(坏习惯)。 因此expression式树被返回而不是结果。

结果发生的是调用代码最终评估结果, An operations error occurred消息仍然出现。 我虽然上面的代码修复无效。 事实上,它确实有,但有代码评估块以外的结果。

简而言之,确保所有访问Active Directory的代码位于该using块内,并且应该修复该exception,并将服务/应用程序部署到服务器。

我现在发现了另一个答案无法添加用户在CRM中的CrmService API,其中指出0x80072020确实是一个权限问题。 我已经改变了我的服务在域级帐户而不是本地系统帐户下运行,这似乎已经解决了我的问题。

当然,这是2年后,我碰到这个,发现以下解决了我的问题:

 using System.Web.Hosting; ... ... // Code here runs as the logged on user using (HostingEnvironment.Impersonate()) { // This code runs as the application pool user DirectorySearcher searcher ... } 

参考

这发生在我在ASP.NET(Windows 2008 R2 / IIS7),我搞乱了Web.config和这个错误开始发生在每个FindByIdentity调用。 根本原因是应用程序池是作为DefaultAppPool运行的,并且一旦我将其更改为以networking服务运行,它就再次开始工作。 我不太明白为什么会改变,但确实如此。

我有同样的问题。 更改应用程序池后,我获得了成功,如下所示:处理模型加载用户configuration文件= true

在我的情况下,Web应用程序池是作为“DefaultAppPool”运行的,它没有足够的访问权限来连接到公司的Active Directory。 所以,我冒充了一个在我的代码中拥有AD访问权限的帐户,一切正常。

如果出现错误代码“发生操作错误(0x80072020)” ,则可能意味着“访问被拒绝”

  • 检查您的Web服务器是否在AD域中
    • 如果没有,则必须将身份validation放入PrincipalContext中。

示例(类似这样的):

 public bool foo(String username, String password) { string ADIPaddress = "[ipaddress]"; ContextOptions options = ContextOptions.Negotiate; PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, AD_IPaddress, null, options, username, password); bool isAuthenticated = principalContext.ValidateCredentials(username, password, options); return isAuthenticated; } 

参考

  • C#网页登入AD网域进行LDAPvalidation

对于我来说,我遇到了同样的问题,试图login到其中一个域控制器,我有2个域控制器,其中1个正在工作,另一个不工作,我相信这与用户configuration文件有关,仍在调查…