始终允许的查询字符串 <alwaysAllowedQueryStrings>

概述

<alwaysAllowedQueryStrings> 元素可指定请求筛选始终允许的查询字符串集合。 <alwaysAllowedQueryStrings> 元素包含一组 <add> 元素,后者指定请求筛选允许的查询字符串模式,这些模式将覆盖 <denyQueryStringSequences> 集合中的值。

兼容性

版本 说明
IIS 10.0 <alwaysAllowedQueryStrings> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <alwaysAllowedQueryStrings> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <alwaysAllowedQueryStrings> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <requestFiltering> 元素的 <alwaysAllowedQueryStrings> 元素作为 IIS 7.5 的功能提供。
IIS 7.0 <requestFiltering> 元素的 <alwaysAllowedQueryStrings> 元素作为 IIS 7.0 的更新引入,可通过 Microsoft 知识库文章 957508 (https://support.microsoft.com/kb/957508) 获取。
IIS 6.0 <alwaysAllowedQueryStrings> 元素大致类似于 URLScan 3.0 新增的 [AlwaysAllowedQueryStrings] 部分。

安装

IIS 7 及更高版本的默认安装包括请求筛选角色服务或功能。 如果卸载请求筛选角色服务或功能,可以使用以下步骤重新安装它。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“请求筛选”。 单击 “下一步”
    Image of Security pane in Server Roles page expanded displaying Request Filtering highlighted.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet Information Services”、“万维网服务”和“安全性”,然后选择“请求筛选”。
    Image of Security and World Wide Web Services pane expanded and Request Filtering option selected.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“请求筛选”,然后单击“下一步”。
    Screenshot of World Wide Web Services and Security pane expanded with Request Filtering selected.
  5. “确认安装选择”页中,单击“安装”
  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“安全性”。
  4. 选择“请求筛选”,然后单击“确定”。
    Image of Internet Information Services and World Wide Web pane displaying Request Filtering selected.

操作方式

如何允许查询字符串序列

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

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

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

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

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

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) 管理器”。
  2. 在“连接”窗格中,转到要为其修改请求筛选设置的连接、站点、应用程序或目录。

  3. 在“主页”窗格中,双击“请求过滤”。

  4. 在“请求筛选”窗格中,单击“查询字符串”选项卡,然后单击“操作”窗格中的“允许查询字符串…”

  5. 在“允许查询字符串”对话框中,输入要阻止的查询字符串序列,然后单击“确定”

配置

<requestFiltering> 元素的 <alwaysAllowedQueryStrings> 元素在站点、应用程序或目录级别配置。

特性

无。

子元素

元素 说明
add 可选元素。 将查询字符串模式添加到请求筛选始终允许的查询字符串集合。
clear 可选元素。

清除请求筛选始终允许的查询字符串模式的集合。
remove 可选元素。

从请求筛选始终允许的查询字符串集合中移除查询字符串模式。

配置示例

下面的示例演示了 <denyQueryStringSequences> 元素和 <alwaysAllowedQueryStrings> 元素的组合,如果它们包含两个特定字符序列中的任一,则会拒绝任何查询字符串,但如果特定查询字符串包含以特定顺序出现的这两个特定字符序列,则将被始终允许。

<system.webServer>
   <security>
      <requestFiltering>
         <denyQueryStringSequences>
            <add sequence="bad" />
            <add sequence="sequence" />
         </denyQueryStringSequences>
         <alwaysAllowedQueryStrings>
            <add queryString="bad=sequence" />
         </alwaysAllowedQueryStrings>
      </requestFiltering>
   </security>
</system.webServer>

代码示例

以下示例演示如何添加默认网站始终允许的查询字符串。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"alwaysAllowedQueryStrings.[queryString='allowed=sequence']"

PowerShell

$alwaysAllowedQueryStrings = Get-IISConfigSection -CommitPath 'Default Web Site' -SectionPath 'system.webServer/security/requestFiltering' | Get-IISConfigCollection -CollectionName 'alwaysAllowedQueryStrings'
New-IISConfigCollectionElement -ConfigCollection $alwaysAllowedQueryStrings -ConfigAttribute @{ 'queryString' = 'allowed=sequence' }

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.GetWebConfiguration("Default Web Site");
         ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
         ConfigurationElementCollection alwaysAllowedQueryStringsCollection = requestFilteringSection.GetCollection("alwaysAllowedQueryStrings");

         ConfigurationElement addElement = alwaysAllowedQueryStringsCollection.CreateElement("add");
         addElement["queryString"] = @"allowed=sequence";
         alwaysAllowedQueryStringsCollection.Add(addElement);

         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.GetWebConfiguration("Default Web Site")
      Dim requestFilteringSection As ConfigurationSection = config.GetSection("system.webServer/security/requestFiltering")

      Dim alwaysAllowedQueryStringsCollection As ConfigurationElementCollection = requestFilteringSection.GetCollection("alwaysAllowedQueryStrings")
      Dim addElement As ConfigurationElement = alwaysAllowedQueryStringsCollection.CreateElement("add")
      addElement("queryString") = "allowed=sequence"
      alwaysAllowedQueryStringsCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

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

var alwaysAllowedQueryStringsCollection = requestFilteringSection.ChildElements.Item("alwaysAllowedQueryStrings").Collection;
var addElement = alwaysAllowedQueryStringsCollection.CreateNewElement("add");
addElement.Properties.Item("queryString").Value = "allowed=sequence";
alwaysAllowedQueryStringsCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set requestFilteringSection = adminManager.GetAdminSection("system.webServer/security/requestFiltering", "MACHINE/WEBROOT/APPHOST/Default Web Site")

Set alwaysAllowedQueryStringsCollection = requestFilteringSection.ChildElements.Item("alwaysAllowedQueryStrings").Collection
Set addElement = alwaysAllowedQueryStringsCollection.CreateNewElement("add")
addElement.Properties.Item("queryString").Value = "allowed=sequence"
alwaysAllowedQueryStringsCollection.AddElement(addElement)

adminManager.CommitChanges()