65.9K
CodeProject 正在变化。 阅读更多。
Home

使用安装程序类和自定义操作配置 IIS 安全设置和不同权限

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6投票s)

2012 年 8 月 14 日

CPOL

3分钟阅读

viewsIcon

29825

downloadIcon

639

使用安装程序类和自定义操作设置 IIS 安全类型和用户、帐户权限

引言

首先,我想衷心感谢 codeproject 和所有贡献者提供的精彩文章,我从这个网站学到了很多东西,因此我真的不知道该用什么语言来表达我的谢意,这是我第一次尝试发表一篇非常小的文章,但我相信这将在开发人员想要以编程方式执行 IIS 设置时有所帮助。

本文帮助开发人员使用安装程序类设置网站的身份验证和权限。

当开发人员对设置移交给部署团队后的部署过程控制较少或根本无法控制时,这确实有助于简化部署过程。

背景

在某些情况下,开发人员希望确保网站必须启用/禁用特定类型的身份验证等,如以下快照所示

我需要满足两个要求

1. 启用 IIS 中的 Windows 身份验证,并禁用所有其他身份验证,如上图所示。

2. Network、Network Service 和 Everyone 应该对该站点具有完全控制权

并且以上两点都应该在无需手动设置的情况下发生,这意味着一旦部署设置完成,工程师就不应该手动更改设置,它们应该自动应用。

在继续之前,我想提一下,我参考了不同的网站和我自己的逻辑才达到这个阶段,所以到目前为止,我还没有相应的 URL 参考,但如果他们碰巧看到了这篇文章,我想感谢他们。

为了执行上述任务,开发人员需要引用两个 dll,即

Microsoft.Web.Management.dll 和 Microsoft.Web.Administration,它们可以在

使用代码

请查找随附的 zip 文件,其中包含所有完整的示例代码、ReadMe.txt 和文档,这些文档建议详细的步骤,展示了如何在设置期间设置虚拟目录的权限。 此外,如何在设置期间设置所需的身份验证。




[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);
    stateSaver.Add("targetvdir", Context.Parameters["targetvdir"].ToString());           
    configureIIS(Context.Parameters["targetvdir"].ToString());
    if (!EventLog.SourceExists("SampleApplication"))
    {
        EventSourceCreationData mySource = 
          new EventSourceCreationData("SampleApplication", "SampleApplicationLogs");
        EventLog.CreateEventSource(mySource);
        EventLog.WriteEntry("SampleApplication", "SampleApplication IIS Settings done.");
        EventLog.WriteEntry("SampleApplication", "targetvdir..." + 
          Context.Parameters["targetvdir"].ToString());
    }
    else
    {
        EventLog.WriteEntry("SampleApplication", "SampleApplication IIS Settings done.");
        EventLog.WriteEntry("SampleApplication", 
          "targetvdir..." + Context.Parameters["targetvdir"].ToString());
    }
    stateSaver.Add("targetdir", Context.Parameters["targetdir"].ToString());
    DirectorySecurity dirSec = Directory.GetAccessControl(@Context.Parameters["targetdir"].ToString());
    FileSystemAccessRule fsar = new FileSystemAccessRule("Everyone",
                                                        FileSystemRights.FullControl,
                                                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                        PropagationFlags.None,
                                                        AccessControlType.Allow);
    dirSec.AddAccessRule(fsar);
    FileSystemAccessRule fNet = new FileSystemAccessRule("NETWORK",
                                                          FileSystemRights.FullControl,
                                                          InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                          PropagationFlags.None,
                                                          AccessControlType.Allow);
    dirSec.AddAccessRule(fNet);
    FileSystemAccessRule fNetServ = new FileSystemAccessRule("NETWORK SERVICE",
                                                        FileSystemRights.FullControl,
                                                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                        PropagationFlags.None,
                                                        AccessControlType.Allow);
    dirSec.AddAccessRule(fNetServ);
    Directory.SetAccessControl(@Context.Parameters["targetdir"].ToString(), dirSec);
}
/// <summary>
/// This method change the security setting of iis for particular web application name specified during the setup 
/// </summary>
/// <param name="vdName"></param>
private void configureIIS(string vdName)
{
    using (ServerManager serverManager = new ServerManager())
    {
        Microsoft.Web.Administration.Configuration config = 
           serverManager.GetApplicationHostConfiguration();
        Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = 
           config.GetSection("system.webServer/security/authentication/anonymousAuthentication", 
           "Default Web Site/" + vdName);
        anonymousAuthenticationSection["enabled"] = false;
        Microsoft.Web.Administration.ConfigurationSection windowsAuthenticationSection = 
           config.GetSection("system.webServer/security/authentication/windowsAuthentication", 
           "Default Web Site/" + vdName);
        windowsAuthenticationSection["enabled"] = true;
        serverManager.CommitChanges();
    }
}

可以很容易地确定 ConfigureIIS 方法可确保设置 Windows 身份验证属性,并且匿名身份验证设置为 false。

为了创建部署,前提条件是开发人员应该安装项目设置的部署类型。 这可以从 这里 下载



安装部署包后,开发人员可以添加项目类型的部署,例如

假设您已经准备好 Web 解决方案,如上图所示,您可以添加项目类型的部署

确保应用程序设置为 Release 模式

构建解决方案。

现在让我们添加安装程序类,可以通过添加类库,然后在其中添加安装程序类来完成。

删除默认类并添加安装程序类,如下面的快照所示;

添加上面显示的代码。
右键单击“解决方案”文件并添加 Web 设置项目

右键单击 Web 设置 -> 添加 -> 项目输出,如图所示

将出现以下弹出窗口,从部署项目和安装程序类添加主输出。下图显示了从安装程序类添加,

点击“确定”。

请以与快照中显示相同的方式从部署项目添加主输出。

点击“确定”。

右键单击 Web 设置项目并添加自定义操作,如下所示

右键单击“自定义操作”中的“安装”节点,然后添加如下所示

单击“属性”或按 F7 键并添加以下自定义操作数据(参考下图)

右键单击解决方案资源管理器中的解决方案文件,然后生成解决方案。

成功构建后。 打开设置文件夹

从 *Release* 文件夹运行设置。

就这样,朋友们..........我们都准备好从设置项目的 Release 文件夹部署应用程序了;

关注点

本文最有趣的部分是了解到我必须找出虚拟目录的安装位置,然后设置该文件夹的权限,因此您可以识别代码的这一部分"

Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration(); 
Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = 
  config.GetSection("system.webServer/security/authentication/anonymousAuthentication", 
  "Default Web Site/" + vdName);
anonymousAuthenticationSection["enabled"] = false;
Microsoft.Web.Administration.ConfigurationSection windowsAuthenticationSection = 
  config.GetSection("system.webServer/security/authentication/windowsAuthentication", 
  "Default Web Site/" + vdName);
windowsAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
© . All rights reserved.