以编程方式修改ACL以为应用程序池提供应用程序的所有权限(IIS)

时间:2018-06-13 10:57:13

标签: iis asp.net-core permissions application-pool asp.net-core-2.1

我有一个使用Microsoft.Web.Administration命名空间中的服务器管理器对象创建应用程序和应用程序池的进程,首先创建应用程序池,然后创建应用程序,将新创建的应用程序池分配给应用程序,代码下方。

protected TResult UseServerManagerWrapper<TResult>(Func<ServerManager, TResult> func)
    {
        using (var serverManager = new ServerManager())
        {
            return func(serverManager);
        }
    }

应用程序创建功能

public void CreateApplication(String siteName, String parentApplicationName, String organisationName, String applicationName, String applicationPoolName)
    {
        UseServerManagerWrapper(serverManager =>
            {
                var site = serverManager.Sites[siteName];

                var newApplication =
                    site.Applications.Add(
                        GetApplicationPath(parentApplicationName, organisationName, applicationName),
                        this.GetGeneratedApplicationPhysicalPath(siteName, parentApplicationName, organisationName, applicationName));

                newApplication.ApplicationPoolName = applicationPoolName;
                serverManager.CommitChanges();
                return true;
            });
    }

和创建应用池。

public Boolean CreateApplicationPool(String applicationPoolName)
    {
        return UseServerManagerWrapper(serverManager =>
            {
                var appPool = serverManager.ApplicationPools.Add(applicationPoolName);

                appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
                appPool.ManagedRuntimeVersion = "";
                serverManager.CommitChanges();
                return true;
            });
    }

这一切都运行正常,我唯一的问题是我必须进入应用程序文件夹并手动为应用程序池分配权限。

我在ServerManager文档中看不到任何可以帮助我的内容,我无法找到使用Directory.SetAccessControl方法来提供应用程序池权限的方法。无论如何在代码中执行此操作?

如果我使用错误的术语或其他任何东西,我很抱歉,我是一般新出版的人。如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

好的,经过大量的搜索和一些反复试验后,我找到了解决方案,而且它与ServerManager对象无关。首先,要在ASP.NET Core 2.1(1.x / 2.x)中使用它,我需要System.IO.FileSystem.AccessControl Nuget和下面的命名空间。

EdgeHubConnectionString

这些可以修改文件和文件夹的ACL,然后CreateApplication函数变为如下。

using System.Security.AccessControl;
using System.Security.Principal;

“newApplication.ApplicationPoolName = applicationPoolName”和“serverManager.CommitChanges()”之间的代码从新生成的目录中获取ACL,从而可以修改它并使用新的FileSystemAccessRule重新分配。