添加安全授权 <add>

概述

<authorization> 集合的 <add> 元素定义一条授权规则,用于允许或拒绝指定用户、组、匿名用户或所有用户的访问。

accessType 属性指定两种类型的授权规则之一:

  • 允许规则定义了可以访问服务器上的站点、应用程序或所有网站的用户帐户或用户组。
  • 拒绝规则定义了无法访问服务器上的站点、应用程序和所有网站的用户帐户或用户组。

还可将授权规则的目标设定为 HTTP 谓词列表和特定用户或组。

兼容性

版本 说明
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 中引入了 <authorization> 集合的 <add> 元素。
IIS 6.0 <authorization> 集合取代了 IIS 6.0 的 AzEnableAzStoreNameAzScopeNameAzImpersonationLevel 元数据库属性。

安装

要支持和配置 Web 服务器上的站点和应用程序的授权,必须安装 URL 授权模块。 为此,请按照以下步骤操作。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。 - 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。 - 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“URL 授权”。 单击 “下一步”
    Screenshot that shows U R L Authorization selected for Windows Server 2012.。 - 在“选择功能”页上,单击“下一步”。 - 在“确认安装选择”页上,单击“安装”。 - 在“结果”页面中单击“关闭”。

Windows 8 或 Windows 8.1

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

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。 - 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。 - 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。 - 在“添加角色服务向导”的“选择角色服务”页上,选择“URL 授权”,然后单击“下一步”。
    Screenshot that shows U R L Authorization selected for Windows Server 2008.在“确认安装选择”页上,单击“安装”。 - 在“结果”页面中单击“关闭”。

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。 - 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。 - 展开“Internet Information Services”,选择“URL 授权”,然后单击“确定”。
    Screenshot that shows U R L Authorization selected for Windows Windows Vista or Windows 7.

操作方式

如何添加授权规则

  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. 在“连接”窗格中,展开服务器名称,展开“站点”,然后导航到要配置授权的站点或应用程序。

  3. 在“主页”窗格中,双击“授权规则”。
    Screenshot that shows the Default Web Site Home pane, with Authorization Rules selected.

  4. 要添加新的授权规则,请在“操作”窗格中单击“添加允许规则...”或“添加拒绝规则...”。

  5. 应用站点或应用程序所需的授权设置,然后单击“确定”。 例如:

    • 示例 1:为所有用户添加特定 HTTP 谓词的“允许”规则:
      Screenshot that shows the Add Deny Authorization Rule dialog box, with Apply this rule to specific verbs selected.

    • 示例 2:为特定用户添加 HTTP 谓词的“拒绝”规则:
      Screenshot that shows the Add Deny Authorization Rule dialog box, with Specified rules selected.

      注意

      若要编辑或删除现有规则,请在“授权规则”窗格中选择该规则,然后在“操作”窗格单击“编辑”或“删除”。 如果单击“编辑...”,将显示一个对话框,用于编辑规则;此对话框类似于“添加允许授权规则”和“添加拒绝授权规则”对话框。

配置

特性

属性 说明
accessType 必需枚举属性。

accessType 属性可以是下列可能值之一。
说明
Allow 指定允许授权的规则。

数值为 0
Deny 指定拒绝授权的规则。

数值为 1
roles 可选的字符串属性。

指定授权规则的角色。
users 可选的字符串属性。

指定授权规则的用户。 可以在逗号分隔的列表中添加多个用户。 此外,还定义了以下特殊标识符。
说明
* 指定规则将应用于所有用户。
? 指定规则将应用于匿名用户。
verbs 可选的字符串属性。

指定授权规则的 HTTP 谓词。

如果此值留空或未指定,规则将应用于所有 HTTP 谓词。

子元素

无。

配置示例

以下配置示例(包含在 Web.config 文件中时)会移除默认的 IIS 授权设置,该设置允许所有用户访问网站或应用程序内容。 然后配置一个授权规则,该规则仅允许具有管理员权限的用户访问内容。

<configuration>
   <system.webServer>
      <security>
         <authorization>
            <remove users="*" roles="" verbs="" />
            <add accessType="Allow" users="" roles="Administrators" />
         </authorization>
      </security>
   </system.webServer>
</configuration>

代码示例

以下示例添加一个允许授权规则,该规则允许管理员组中的用户访问名为 Contoso 的网站。

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/security/authorization /+"[accessType='Allow',roles='administrators']"

注意

使用 AppCmd.exe 配置这些设置时,可以选择将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分,而不是提交到 Web.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.GetWebConfiguration("Contoso");
         ConfigurationSection authorizationSection = config.GetSection("system.webServer/security/authorization");
         ConfigurationElementCollection authorizationCollection = authorizationSection.GetCollection();

         ConfigurationElement addElement = authorizationCollection.CreateElement("add");
         addElement["accessType"] = @"Allow";
         addElement["roles"] = @"administrators";
         authorizationCollection.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("Contoso")
      Dim authorizationSection As ConfigurationSection = config.GetSection("system.webServer/security/authorization")
      Dim authorizationCollection As ConfigurationElementCollection = authorizationSection.GetCollection
      Dim addElement As ConfigurationElement = authorizationCollection.CreateElement("add")
      addElement("accessType") = "Allow"
      addElement("roles") = "administrators"
      authorizationCollection.Add(addElement)
      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";
var authorizationSection = adminManager.GetAdminSection("system.webServer/security/authorization", "MACHINE/WEBROOT/APPHOST/Contoso");
var authorizationCollection = authorizationSection.Collection;

var addElement = authorizationCollection.CreateNewElement("add");
addElement.Properties.Item("accessType").Value = "Allow";
addElement.Properties.Item("roles").Value = "administrators";
authorizationCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"
Set authorizationSection = adminManager.GetAdminSection("system.webServer/security/authorization", "MACHINE/WEBROOT/APPHOST/Contoso")
Set authorizationCollection = authorizationSection.Collection

Set addElement = authorizationCollection.CreateNewElement("add")
addElement.Properties.Item("accessType").Value = "Allow"
addElement.Properties.Item("roles").Value = "administrators"
authorizationCollection.AddElement(addElement)

adminManager.CommitChanges()