.net System.DirectoryServices.DirectoryServicesCOMException:发生操作错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13688031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred
提问by Vinicius Ottoni
I have the same web app working in three others servers. Anyone have any idea why is not working in the 4th server? See the error and stacktrace:
我在其他三台服务器上运行相同的网络应用程序。任何人都知道为什么不能在第四台服务器上工作?查看错误和堆栈跟踪:
An operations error occurred.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:
System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[DirectoryServicesCOMException (0x80072020): An operations error occurred. ] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +454 System.DirectoryServices.DirectoryEntry.Bind() +36 System.DirectoryServices.DirectoryEntry.get_AdsObject() +31 System.DirectoryServices.PropertyValueCollection.PopulateList() +22
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +96
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +142 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +1134 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +124 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +31 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable'1 identityType, String identityValue, DateTime refDate) +14
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue) +73
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +25
Infraero.TINE3.STTEnterprise.Web.Common.Seguranca.ServicoAutenticacao.EfetuarLogin(AcessoUsuario acessoUsuario, String senha) in D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Common\Seguranca\ServicoAutenticacao.cs:34 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController.ValidarUsuarioAD(String matricula, String senha, AcessoUsuario acessoUsuario) in D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:92 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController.ValidarUsuario(String matricula, String senha) in D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:80 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController.Index(LoginViewModel loginViewModel) in D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:54 lambda_method(Closure , ControllerBase , Object[] ) +108
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary'2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary'2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func'1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList'1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8'1.b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult'1.End() +62 System.Web.Mvc.<>c__DisplayClasse.b__d() +50
System.Web.Mvc.SecurityUtil.b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
发生操作错误。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:
System.DirectoryServices.DirectoryServicesCOMException:发生操作错误。源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。
堆栈跟踪:
[DirectoryServicesCOMException (0x80072020): 发生操作错误。] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +454 System.DirectoryServices.DirectoryEntry.Bind() +36 System.DirectoryServices.DirectoryEntry.get_AdsObject() +31 System.DirectoryServices.PropertyValueCollection.PopulateList() +22
System.DirectoryServices .PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +96
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +142 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +1134 System.DirectoryServices.AccountManagement.PrincipalContext.DomainInit() +37 System.DirectoryServices.AccountManagement.PrincipalContext.InitialContext +124 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +31 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable'1 identityType, String identityValue, DateTime refDate) +14
System.DirectoryServices.AccountManage Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue) +73
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +25
Infraero.TINE3.STTEnterprise.Web.Common.Seguranca.ServicoAutenticacao.EfetuarLogin(AcessoUsuario acessoUsuario, String senha) 在 D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRCINE\3.STTeroT Seguranca\ServicoAutenticacao.cs:34 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController.ValidarUsuarioAD(String matricula, String senha, AcessoUsuario acessoUsuario) 在 D:\SVN\STT\trunk\4-0_CodigoS\Enterprise.Web.Controllers.LoginController.ValidarUsuarioAD TINE3.STTEnterprise.Web\Controllers\LoginController.cs:92 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController.ValidarUsuario(String matricula, String senha) 在 D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC \Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:80 Infraero.TINE3.STTEnterprise.Web.Controllers.LoginController。D:\SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:54 lambda_method(Closure , ControllerBase , Object[] )中的索引(LoginViewModel loginViewModel) +108
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary'2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod( ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary'2个参数) +27
System.Web.Mvc.<>c__DisplayClass15.b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func'1 continuation) + 263
System.Web.Mvc.<>c__DisplayClass17.b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList'1 过滤器, ActionDescriptor actionDescriptor, IDictionary`2 参数) +191
System.Web.Mvc.ControllerActionInvoker .InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute( RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web .Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8'1.b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult'1.End() +62 System.Web.Mvc.<>c__DisplayClasse.b__d( ) +50
System.Web.Mvc.SecurityUtil.b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System. Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&同步完成)+184
EfetuarLogin Method:
Efetuar登录方法:
public static bool EfetuarLogin(User user, string password)
{
bool isValid = false;
if (user != null)
{
PrincipalContext context = new PrincipalContext(ContextType.Domain);
using (context)
{
isValid = context.ValidateCredentials(user.Login, password);
if (isValid)
{
UserPrincipal userAD = UserPrincipal.FindByIdentity(context, user.Login);
MySession.CurrentUser = new MyUserSession()
{
Id = user.Id,
ProfileId = user.ProfileId ,
Login = user.Login ,
Name = userAD.Name
};
}
}
}
return isValid;
}
回答by Neville
I had exactly the same error and fixed it by changing the site's application pool to run under the Network Service.
我遇到了完全相同的错误,并通过将站点的应用程序池更改为在网络服务下运行来修复它。
In IIS:
在 IIS 中:
- Select your site's application pool
- Select Advanced Settings on the right-hand side
- On the Advanced Settings pop-up window, scroll down to the Process Model group
- Change the first option called Identity to NetworkService (mine was set to the default ApplicationPoolIdentity).
- 选择您站点的应用程序池
- 选择右侧的高级设置
- 在“高级设置”弹出窗口中,向下滚动到“流程模型”组
- 将名为 Identity 的第一个选项更改为 NetworkService(我的设置为默认的 ApplicationPoolIdentity)。
I hope this helps.
我希望这有帮助。
回答by Matt67
I know this topic is old but just for future people who will be looking for this issue Just use this method to execute the code with Elevate privileges
我知道这个话题很老,但仅供将来寻找此问题的人使用只需使用此方法以提升权限执行代码
using (HostingEnvironment.Impersonate()) {
// This code runs as the application pool user
}
回答by Joe
There isn't an InnerExceptionin this case, it's just wrapping a COM error.
没有一个InnerException在这种情况下,它只是一个包装COM错误。
Almost certainly it's because your Application Pool identity does not have permission to access Active Directory.
几乎可以肯定,这是因为您的应用程序池身份没有访问 Active Directory 的权限。
回答by Tony L.
In my case, switching from ApplicationPoolItentity to NetworkService in the app pool did work BUTit not preferred "because services running as Network Service can tamper with other services that run under the same identity" per the following link: (http://www.iis.net/learn/manage/configuring-security/application-pool-identities).
在我的情况下,在应用程序池中从 ApplicationPoolItentity 切换到 NetworkService 确实有效,但它不喜欢“因为作为网络服务运行的服务可以篡改以相同身份运行的其他服务”,每个链接如下:(http://www. iis.net/learn/manage/configuring-security/application-pool-identities)。
I ran the hotfix (KB2545850) on the server and rebooted per this answer:(DirectoryServicesCOMException 80072020 From IIS 7.5 Site Running Under ApplicationPoolIdentity)
我在服务器上运行了修补程序 ( KB2545850) 并根据此答案重新启动:( DirectoryServicesCOMException 80072020 From IIS 7.5 Site Running Under ApplicationPoolIdentity)
It appears to be working well now.
它现在似乎运行良好。
Background on my task: Upgrading apps from .net framework 2.0 on Server 2003 to .net framework 4.0 on Server 2008 R2.
我的任务背景:将应用程序从 Server 2003 上的 .net framework 2.0 升级到 Server 2008 R2 上的 .net framework 4.0。
回答by Umair Akbar
My Experience was little different with this Error. I had to move on-premise application to Azure, where the LDAP call was happening from on-premise, but not from Azure even after opening the required firewall.
我的经验与此错误几乎没有什么不同。我不得不将本地应用程序移动到 Azure,其中 LDAP 调用是从本地发生的,但即使在打开所需的防火墙之后也不会从 Azure 调用。
I tried all solution mentioned above, but none of them was helpful. Network service was already selected on Azure VM.
我尝试了上面提到的所有解决方案,但没有一个有帮助。已在 Azure VM 上选择网络服务。
After lot of hit and trial and research. I fixed it.
经过大量的尝试和研究。我修好了它。
Solution: On-Premise server was having permission to access LDAP and did not required any UserName and Password. But on Azure, you need to specifically make LDAP call with username and Password. Below is the code which helped.
解决方案:本地服务器有权访问 LDAP,不需要任何用户名和密码。但是在 Azure 上,您需要专门使用用户名和密码进行 LDAP 调用。下面是帮助的代码。
var directoryEntry= new DirectoryEntry(adspath, Username, Password)
回答by nPcomp
1 - Change application pool to run under the Network Service.
2 - Click on the Authentication and disable ASP.Net impersonation.
1 - 将应用程序池更改为在网络服务下运行。
2 - 单击身份验证并禁用 ASP.Net 模拟。
回答by CodeWarrior
So if you place a breakpoint on the line:
因此,如果您在该行上放置一个断点:
UserPrincipal userAD = UserPrincipal.FindByIdentity(context, user.Login);
and step through it, it generates the above exception which does not have any InnerExceptions?
并逐步执行它,它会生成上面没有任何 InnerExceptions 的异常?
According to the stack trace, that line is the beginning of the problem. The returned exception shouldhave at least some other information in it as to why it was thrown.
根据堆栈跟踪,该行是问题的开始。返回的异常应该至少包含一些其他关于它为什么被抛出的信息。
InnerException Concatenator
内部异常连接器
The following method takes the top level exception and returns a tab and linebreak formatted breakdown of the inner exceptions as a string.
以下方法采用顶级异常并以字符串形式返回内部异常的制表符和换行符格式的细分。
private static string InnerExceptionConcatenator(Exception ex, int tabTracker = 0)
{
string retVal = "";
if (ex.InnerException != null)
{
tabTracker ++;
retVal = string.Format( "{0}\r\n{1}{2}", ex.Message, new String('\t', tabTracker), InnerExceptionConcatenator(ex.InnerException));
}
else
{
retVal = ex.Message;
}
return retVal;
}
You can call it thusly:
你可以这样称呼它:
try
{
}
catch(ex Exception)
{
var exceptionString = InnerExceptionConcatenator(ex);
var path = @"c:\temp\exception.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(exceptionString);
}
}
else
{
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(exceptionString);
}
}
}

