C# DirectoryEntry IIS访问权限

C# DirectoryEntry IIS访问权限,c#,windows,security,iis,iis-8,C#,Windows,Security,Iis,Iis 8,我有一个控制台应用程序,其中列出了IIS中的网站绑定 using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + GetWebSiteId())) { var bindings = directoryEntry.Properties["ServerBindings"]; } 我通过进程从ASP.NET调用此控制台应用程序 var process = new Process { StartIn

我有一个控制台应用程序,其中列出了IIS中的网站绑定

using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + GetWebSiteId())) {
    var bindings = directoryEntry.Properties["ServerBindings"]; 
}
我通过进程从ASP.NET调用此控制台应用程序

var process = new Process {
   StartInfo = new ProcessStartInfo {
       FileName = "c:/app.exe",
       Arguments = "check",
       UseShellExecute = false,
       RedirectStandardOutput = true,
       CreateNoWindow = true
    }
};
Widows 7/IIS 7.5下的开发机器上一切正常,但当我在Windows 2012/IIS 8上测试时,我发现“访问被拒绝”错误

错误日志

"System.Runtime.InteropServices.COMException (0x80070005): Access is denied.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_IsContainer()
at System.DirectoryServices.DirectoryEntries.ChildEnumerator..ctor(DirectoryEntry container)
at System.DirectoryServices.DirectoryEntries.GetEnumerator()
at IISSubdomainManagement.Program.GetWebSiteId()
at IISSubdomainManagement.Program.TotalBindings()
at IISSubdomainManagement.Program.Main(String[] args)"
p、 的应用程序池标识为“ApplicationPoolIdentity”



我忘了提到,当我从CMD运行我的控制台应用程序时,它在我的服务器上运行良好。你需要授予IUSR帐户访问和执行
C:\app.exe
。应为您提供找到正确帐户所需的信息。

您可能已将权限授予“ApplicationPoolIdentity”,而不是实际对应于该应用程序池的虚拟帐户。阅读或在线搜索虚拟身份IIS等

在您的开发机器上,您可能拥有某种完全的管理员权限,因此它没有受到限制


如果在此之后仍然存在问题,我建议您通过运行复制错误,这样您就可以确切地看到哪个进程正在使用哪个身份访问哪个资源。但是,我建议在开发机器上复制该问题,而不是在生产机器上运行Process Monitor。要想高效地运行它需要一点学习。

在IIS 7/8中,进入控制面板/程序和功能/打开或关闭Windows功能,并检查所有项目来自:Web管理工具(包括:IIS管理服务,II 6管理兼容性)

这个解决方案对我有效==>


我以前有过这个问题,如果我记得的话,问题是它在哪里查找广告服务器,或者凭据没有正确的权限。这很烦人,因为它在本地工作正常。iIUSR和IIS_IUSR对app.exe有完全的安全控制,但仍然不工作。我可以确认当IIS_IUSR没有访问权限时得到的COMException是
System.DirectoryServices.DirectoryServicesCOMException(0x80072020):发生操作错误。
(而不是
拒绝访问
)完成后,我将应用程序池创建的虚拟身份添加为app.exe上的完全安全权限,但当我选择“LocalSystem”时仍然存在该错误作为应用程序池标识,一切正常,但为什么我不能,因为ApplicationPoolIdentityLocalSystem是一个真正的标识,其中一个是特例。我更新了主要评论,并对下一步的故障排除提出了其他建议。