问题:当创建新的 IIS 应用程序池并将其设置为使用应用程序池标识来获取权限时,我不确定如何将这些标识添加到用户组(例如管理员或性能计数器用户)。
的背景:我目前正在编写一个 C#.NET 库,它使用 Microsoft.Web.Administration 来执行以下操作:
- 检测是否安装了 IIS 7.x,如果安装了,则检测哪些组件。
- 将 IIS 7.x 安装或升级到提供的所需组件列表。
- 通过 IIS 创建/管理一个或多个网站。
- 自动为每个网站创建/管理一个应用程序池
上下文是,可执行安装程序将使用此库来在 Windows Server 操作系统上提供 Web 服务器和网站/服务的自动部署,作为大型软件部署的一部分。到目前为止,除了需要在应用程序池/网站创建上执行的一些权限的自动化之外,上述所有内容都已实现、测试并且(大部分)正常运行。
在我安装新网站的方法中,我创建了一个新的应用程序池并强制它使用应用程序池标识:
static public void InstallSite(string name, string path, int port)
{
Site site;
var appPoolName = ApplicationPoolBaseName + name;
using (var iisManager = new ServerManager())
{
// Set up a custom application pool for any site we run.
if (!iisManager.ApplicationPools.Any(pool => pool.Name.Equals(appPoolName)))
{
iisManager.ApplicationPools.Add(appPoolName);
iisManager.ApplicationPools[appPoolName].ManagedRuntimeVersion = "v4.0";
}
iisManager.CommitChanges();
}
// ... other code here ('site' gets initialized) ...
using (var iisManager = new ServerManager())
{
// Set anonymous auth appropriately
var config = iisManager.GetWebConfiguration(site.Name);
var auth = config.GetSection("system.web/authentication");
auth.SetMetadata("mode", "Windows");
var authSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication");
authSection.SetAttributeValue("enabled", true);
authSection.SetAttributeValue("userName", string.Empty); // Forces the use of the Pool's Identity.
authSection = config.GetSection("system.webServer/security/authentication/basicAuthentication");
authSection.SetAttributeValue("enabled", false);
authSection = config.GetSection("system.webServer/security/authentication/digestAuthentication");
authSection.SetAttributeValue("enabled", false);
authSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication");
authSection.SetAttributeValue("enabled", false);
iisManager.CommitChanges();
}
// ... other code here ...
}
据我了解,这将是最佳的安全实践,然后我将向特定网站添加除最小系统访问之外的任何权限。此过程的一部分是将这些应用程序池标识添加到用户组,例如管理员或性能监视器用户。这就是出现并发症的地方。
Now, as 记录在别处 http://forums.iis.net/t/1156692.aspx,每个应用程序池标识以以下格式存在IIS AppPool\\<pool_name>
但是这个假用户没有通过正常的 GUI 用户管理控件列出,并且似乎无法通过诸如System.DirectoryServices.AccountManagement
当跟随这个例子关于SO https://stackoverflow.com/questions/12843469/adding-local-user-to-local-admin-group。此外,有关应用程序池身份的其他问题似乎与从子网站内引用它 https://stackoverflow.com/questions/10101162/get-the-application-pool-identity-programmatically,而不是来自安装上下文。
那么,有谁知道正确的方法是什么
- a) 以编程方式引用和访问应用程序池标识。
- b) 通过添加用户组来授予应用程序池身份权限。