c# - 以编程方式将 IIS 应用程序池标识 "users"分配给组

标签 c# permissions iis-7.5 application-pool applicationpoolidentity

问题:当创建新的 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 ...
}

据我所知,这将是最好的安全做法,然后我会添加对特定网站的权限,以实现最低限度的系统访问权限。此过程的一部分是将这些应用程序池身份添加到用户组,例如管理员或性能监视器用户。这就是并发症出现的地方。

现在,作为documented elsewhere , 每个应用程序池标识以 IIS AppPool\\<pool_name> 的格式存在但是这个伪用户没有通过普通的 GUI 用户管理控件列出,并且似乎无法通过诸如 System.DirectoryServices.AccountManagement 之类的库访问。当关注 this example on SO .此外,有关应用程序池标识的其他问题似乎与 referencing it from within a child website 有关。 ,而不是在安装上下文中。

所以,有谁知道正确的方法是什么

  • a) 以编程方式引用和访问应用程序池标识。
  • b) 通过添加用户组来授予应用程序池标识权限。

最佳答案

感谢您提出的问题。这正是我昨晚试图解决的问题,它给了我足够的继续,我终于能够拼凑出一个仅使用托管代码的答案。我发现通过三个步骤让框架找到并使用虚拟用户:

  • 使用new System.Security.Principal.NTAccount(@"IIS APPPOOL\<appPoolName>")处理该帐户。
  • 使用.Translate(typeof (System.Security.Principal.SecurityIdentifier))将其转换为 SID
  • 了解Principal.FindByIdentity()将该 SID 视为一个组,而不是一个用户

最终的工作程序(我测试的是Windows Server 2012)如下:

using System;
using System.DirectoryServices.AccountManagement;

namespace WebAdminTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var user = new System.Security.Principal.NTAccount(@"IIS APPPOOL\10e6c294-9836-44a9-af54-207385846ebf");
            var sid = user.Translate(typeof (System.Security.Principal.SecurityIdentifier));

            var ctx = new PrincipalContext(ContextType.Machine);

            // This is weird - the user SID resolves to a group prinicpal, but it works that way.
            var appPoolIdentityGroupPrincipal = GroupPrincipal.FindByIdentity(ctx, IdentityType.Sid, sid.Value);

            Console.WriteLine(appPoolIdentityGroupPrincipal.Name);
            Console.WriteLine(appPoolIdentityGroupPrincipal.DisplayName);

            GroupPrincipal targetGroupPrincipal = GroupPrincipal.FindByIdentity(ctx, "Performance Monitor Users");

            // Making appPoolIdentity "group" a member of the "Performance Monitor Users Group"
            targetGroupPrincipal.Members.Add(appPoolIdentityGroupPrincipal);
            targetGroupPrincipal.Save();

            Console.WriteLine("DONE!");
            Console.ReadKey();
        }
    }
}

关于c# - 以编程方式将 IIS 应用程序池标识 "users"分配给组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14712916/

相关文章:

c# - 分开持有项目部分是一种好方式吗

c# - 从 asp.net 核心应用程序启动 linux 进程失败(找不到文件)

c# - IIS 7.5 无法使用代码隐藏文件加载自定义 HTTP 处理程序

c# - 构造函数的单元测试

c# - 如何正确地将结构数组从 MQL4 传递到 C# DLL 库

cocoa - NSFileManager -copyItemAtPath :toPath:error: fails with read-only folder

design-patterns - 设计网站权限方案的资源

linux - 当我尝试使用 "make"安装某些东西时,为什么我的权限被拒绝?

asp.net - 基于多个(但不是全部)条件的 IIS 重定向规则

php - IIS 7.5 在哪里记录错误?