添加 IP 安全性 <add>

概述

<ipSecurity> 集合的 <add> 元素定义唯一的 IP 安全限制。 每个限制可以基于 IPv4 地址、IPv4 地址范围或 DNS 域名。

兼容性

版本 说明
IIS 10.0 <add> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <add> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <add> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <add> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 IIS 7.0 中引入了 <ipSecurity> 集合的 <add> 元素。
IIS 6.0 <ipSecurity> 集合替代了 IIS 6.0 IPSecurity 元数据库属性。

安装

IIS 的默认安装不包括 IP 安全的角色服务或 Windows 功能。 若要在 IIS 上使用 IP 安全,必须使用以下步骤安装角色服务或 Windows 功能:

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“IP 和域限制”。 单击 “下一步”
    Screenshot that shows I P and Domain Restrictions selected for Windows Server 2012.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet 信息服务”、“万维网服务”和“安全性”,然后选择“IP 安全”。
    Screenshot that shows I P Security selected for Windows 8.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。

  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。

  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。

  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“IP 和域限制”,然后单击“下一步”。

    Screenshot that shows I P and Domain Restrictions selected for Windows Server 2008.

  5. “确认安装选择”页中,单击“安装”

  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。

  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。

  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“安全性”。

  4. 选择“IP 安全”,然后单击“确定”

    Screenshot that shows I P Security selected for Windows Vista or Windows 7.

操作方式

如何添加 IP 限制以拒绝网站访问

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,展开服务器名称,展开“站点”,然后展开要为其添加 IP 限制的站点、应用程序或 Web 服务。

  3. 在“主页”窗格中,双击“IP 地址和域限制”功能。
    Screenshot that shows the Home pane in I I S Manager.

  4. 在“IP 地址和域限制”功能中的“操作”窗格中,单击“添加拒绝条目...”。
    Screenshot that shows the I P Address and Domain Restrictions pane.

  5. 输入要拒绝的 IP 地址,然后单击“确定”。
    Screenshot that shows the Add Deny Restriction Rule dialog box.


如何编辑网站的 IP 限制功能设置

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,展开服务器名称,展开“站点”,然后展开要为其添加 IP 限制的站点、应用程序或 Web 服务。

  3. 在“主页”窗格中,双击“IP 地址和域限制”功能。
    Screenshot that shows the Home pane in Internet Information Services Manager.

  4. 在“IP 地址和域限制”功能中的“操作”窗格中,单击“编辑功能设置...”。
    Screenshot that shows the I P Address and Domain Restrictions window.

  5. 为未指定客户端选择默认访问行为,指定是否按域名启用限制,指定是否启用“代理模式”,选择“拒绝操作类型”,然后单击“确定”
    Screenshot that shows the Add Deny Restriction Rule dialog box. Forbidden is selected under Deny Action Type.

配置

规则按列表中显示的顺序从上到下进行处理。 最后处理 allowunlisted 属性。 关于 Internet 协议安全性 (IPsec) 限制,最佳做法是先列出拒绝规则。

特性

属性 说明
allowed 可选布尔属性。

指定是否允许访问地址空间。

默认值为 false
domainName 可选的字符串属性。

指定要对其施加限制规则的域名。 可以使用星号 (*) 作为通配符。
ipAddress 可选的字符串属性。

指定要对其施加限制规则的 IPv4 地址。
subnetMask 可选的字符串属性。

指定要评估此限制规则的 IP 地址的子网掩码。 可以使用子网掩码来标识地址空间中的 IP 地址范围。 默认值要求与要评估的 IP 地址直接匹配(实际上是单个地址的范围)。

默认值为 255.255.255.255

子元素

无。

配置示例

以下配置示例将两个 IP 限制添加到默认网站;第一个限制拒绝 IP 地址 192.168.100.1 的访问,第二个限制拒绝整个 169.254.0.0 网络的访问。

<location path="Default Web Site">
   <system.webServer>
      <security>
         <ipSecurity>
            <add ipAddress="192.168.100.1" />
            <add ipAddress="169.254.0.0" subnetMask="255.255.0.0" />
         </ipSecurity>
      </security>
   </system.webServer>
</location>

代码示例

以下代码示例将两个 IP 限制添加到默认网站;第一个限制拒绝 IP 地址 192.168.100.1 的访问,第二个限制拒绝整个 169.254.0.0 网络的访问。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/security/ipSecurity /+"[ipAddress='192.168.100.1',allowed='False']" /commit:apphost

appcmd.exe set config "Default Web Site" -section:system.webServer/security/ipSecurity /+"[ipAddress='169.254.0.0',subnetMask='255.255.0.0',allowed='False']" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");
         ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

         ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
         addElement["ipAddress"] = @"192.168.100.1";
         addElement["allowed"] = false;
         ipSecurityCollection.Add(addElement);

         ConfigurationElement addElement1 = ipSecurityCollection.CreateElement("add");
         addElement1["ipAddress"] = @"169.254.0.0";
         addElement1["subnetMask"] = @"255.255.0.0";
         addElement1["allowed"] = false;
         ipSecurityCollection.Add(addElement1);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim ipSecuritySection As ConfigurationSection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site")
      Dim ipSecurityCollection As ConfigurationElementCollection = ipSecuritySection.GetCollection

      Dim addElement As ConfigurationElement = ipSecurityCollection.CreateElement("add")
      addElement("ipAddress") = "192.168.100.1"
      addElement("allowed") = False
      ipSecurityCollection.Add(addElement)

      Dim addElement1 As ConfigurationElement = ipSecurityCollection.CreateElement("add")
      addElement1("ipAddress") = "169.254.0.0"
      addElement1("subnetMask") = "255.255.0.0"
      addElement1("allowed") = False
      ipSecurityCollection.Add(addElement1)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var ipSecuritySection = adminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var ipSecurityCollection = ipSecuritySection.Collection;

var addElement = ipSecurityCollection.CreateNewElement("add");
addElement.Properties.Item("ipAddress").Value = "192.168.100.1";
addElement.Properties.Item("allowed").Value = false;
ipSecurityCollection.AddElement(addElement);

var addElement1 = ipSecurityCollection.CreateNewElement("add");
addElement1.Properties.Item("ipAddress").Value = "169.254.0.0";
addElement1.Properties.Item("subnetMask").Value = "255.255.0.0";
addElement1.Properties.Item("allowed").Value = false;
ipSecurityCollection.AddElement(addElement1);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set ipSecuritySection = adminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set ipSecurityCollection = ipSecuritySection.Collection

Set addElement = ipSecurityCollection.CreateNewElement("add")
addElement.Properties.Item("ipAddress").Value = "192.168.100.1"
addElement.Properties.Item("allowed").Value = False
ipSecurityCollection.AddElement(addElement)

Set addElement1 = ipSecurityCollection.CreateNewElement("add")
addElement1.Properties.Item("ipAddress").Value = "169.254.0.0"
addElement1.Properties.Item("subnetMask").Value = "255.255.0.0"
addElement1.Properties.Item("allowed").Value = False
ipSecurityCollection.AddElement(addElement1)

adminManager.CommitChanges()