我想使用 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];
谢谢!
最佳答案
不,没有解决方法可以在不引起重大安全问题的情况下读取配置文件。你想达到什么目的?
如果读取配置设置,您可以使用同一 DLL 中的 API,该 DLL 将为您提供该站点设置的只读配置访问权限,例如仅读取该站点的 web.config 或 applicationHost.config 中的值,而不是读取该站点的 web.config 或 applicationHost.config 中的值。加密的(例如密码)。该API称为WebConfigurationManager,并有一个名为GetSection的静态方法,例如WebConfigurationManager.GetSection("system.webServer/defaultDocument")
但是,有几个设置(即用于启动进程 w3wp.exe 的所有设置)无法通过该 API 读取。 简短的故事:不幸的是,出于安全原因,许多这些设置无法从工作进程中读取。您可以使用服务器变量读取一些内容,例如 Request.ServerVariables["APP_POOL_ID"]) 或 Request.ServerVariables["APP_POOL_CONFIG"])。当然,您可以计算指针的大小(4 或 8),或使用环境变量(如 PROCESSOR_ARCHITECTURE)
更长的故事:在 IIS 中,出于安全原因,我们采用 applicationHost.config 文件,并将其拆分为较小的 application pool.config 文件(默认情况下位于 C:\inetpub\temp\appPools)它们出于安全原因而被隔离,因此即使不受信任的代码在进程(w3wp.exe)中运行以尝试窃取/读取其他站点的设置,这在物理上也是不可能的。您可以打开该文件并查看其中有哪些设置,并且可以读取这些设置。您会注意到 appPools 部分完全丢失,因为它仅被 WAS 用于启动 w3wp.exe。
关于asp.net - 我可以在没有管理员用户的情况下使用 Microsoft.Web.Administration 中的 ServerManager 作为应用程序池标识吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119488/