1

我想使用 Microsoft.Web.Administration.dll 中的 ServerManager 对象读取应用程序池的一些设置。问题是它只有在应用程序池的身份是具有管理员权限的 Windows 用户时才有效。否则我会得到 UnauthorizedAccessException - Filename: redirection.config; 错误:由于权限不足,无法读取配置文件。有没有关于这个问题的解决方法。我的代码如下:

        ServerManager manager = new ServerManager();
        string currentSiteName = System.Web.Hosting.HostingEnvironment.SiteName;
        Site currentSite = manager.Sites[currentSiteName];
        string appVirtaulPath = HttpRuntime.AppDomainAppVirtualPath;

        string appPoolName = string.Empty;
        foreach (Application app in currentSite.Applications)
        {
            string appPath = app.Path;
            if (appPath == appVirtaulPath)
            {
                appPoolName = app.ApplicationPoolName;
            }
        }

        ApplicationPool currentAppPool = manager.ApplicationPools[appPoolName];

谢谢!

4

1 回答 1

2

不,没有解决方法来读取配置文件而不引起大的安全问题。你想达到什么目的?

如果读取配置设置,您可以在同一个 DLL 中使用 API,该 API 将为您提供对该站点设置的只读配置访问权限,例如仅读取该站点的 web.config 或 applicationHost.config 中的值,而不是加密的(例如密码)。该 API 名为 WebConfigurationManager,并有一个名为 GetSection 的静态方法,例如 WebConfigurationManager.GetSection("system.webServer/defaultDocument")

请参阅:https ://msdn.microsoft.com/en-us/library/microsoft.web.administration.webconfigurationmanager.getsection.aspx

但是,无法通过该 API 读取一些设置(即用于启动进程 w3wp.exe 的所有设置)。小故事:不幸的是,出于安全原因,许多这些设置无法从工作进程中读取。您可以使用服务器变量读取一些内容,例如 Request.ServerVariables["APP_POOL_ID"]) 或 Request.ServerVariables["APP_POOL_CONFIG"])。当然位数可以计算指针的大小(4 或 8),或使用环境变量(如 PROCESSOR_ARCHITECTURE)

更长的故事:在 IIS 中,出于安全原因,我们获取 applicationHost.config 文件并将其拆分为较小的应用程序 pool.config 文件(默认位于 C:\inetpub\temp\appPools),这些文件出于安全原因被隔离,这样即使不受信任的代码将在进程 (w3wp.exe) 中运行,以尝试窃取/读取其他站点的设置,这在物理上是不可能的。您可以打开文件并查看其中有哪些设置,然后您可以阅读这些设置。您会注意到 appPools 部分完全丢失了,因为它仅被 WAS 用于启动 w3wp.exe。

于 2015-08-20T22:29:34.877 回答