匿名身份验证 <anonymousAuthentication>

概述

<anonymousAuthentication> 元素控制 Internet Information Services (IIS) 7 如何处理匿名用户发出的请求。 可以修改 <anonymousAuthentication> 元素以禁用匿名身份验证,也可以将 Internet Information Services (IIS) 配置为使用自定义用户帐户来处理匿名请求。

匿名身份验证会在不提示用户输入用户名或密码的情况下为其提供对你的网站或 FTP 站点的公共区域的访问权限。 默认情况下,IUSR 帐户用于允许匿名访问,该帐户是在 IIS 7.0 中引入的,并取代了 IIS 6.0 的 IUSR_computername 帐户。 应用程序是一组文件,通过协议(如 HTTP)提供内容或提供服务。 在 IIS 中创建应用程序时,应用程序的路径将成为站点 URL 的一部分。

IIS 7 默认使用匿名身份验证。 对于要启用其他身份验证方法(如基本身份验证或 Windows 身份验证)的任何网站、Web 应用程序或 Web 服务,必须禁用匿名身份验证。

兼容性

版本 说明
IIS 10.0 <anonymousAuthentication> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <anonymousAuthentication> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <anonymousAuthentication> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <anonymousAuthentication> 元素未在 IIS 7.5 中进行修改。
IIS 7.0 <anonymousAuthentication> 元素是在 IIS 7.0 中引入的。
IIS 6.0 <anonymousAuthentication> 元素取代了 IIS 6.0 的 AuthFlagsAnonymousUserNameAnonymousUserPassword 源数据库属性。

安装

<anonymousAuthentication> 元素包含在 IIS 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. 在“连接”窗格中,展开服务器名称,展开“站点”,然后转到要配置的层次结构窗格中的级别,然后单击网站或 Web 应用程序。

  3. 滚动到“主页”窗格中的“安全性”部分,然后双击“身份验证”。

  4. 在“身份验证”窗格中,选择“匿名身份验证”,然后在“操作”窗格中单击“禁用”。
    Screenshot of the Authentication pane with Anonymous Authentication selected.

如何从 IUSR 帐户更改匿名身份验证凭据

  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. 在“连接”窗格中,展开服务器名称,展开“站点”,然后导航到要配置的层次结构窗格中的级别,然后单击网站或 Web 应用程序。

  3. 滚动到“主页”窗格中的“安全性”部分,然后双击“身份验证”。

  4. 在“身份验证”窗格中,选择“匿名身份验证”,然后在“操作”窗格中单击“编辑...”。

  5. 在“编辑匿名身份验证凭据”对话框中,执行以下操作之一:

    • 选择“应用程序池标识”以使用应用程序池的标识集,然后单击“确定”。
      Screenshot of setting Anonymous user identity to Application pool identity.

    • 单击“设置...”,然后在“设置凭据”对话框中,在“用户名”框中输入帐户的用户名,在“密码”和“确认密码”框中输入帐户的密码,单击“确定”,然后再次单击“确定”。
      Screenshot of a blank Set Credentials dialog.

      注意

      如果使用此过程,请仅授予新帐户对 IIS 服务器计算机的最低权限。

配置

<anonymousAuthentication> 元素可在 Web.config 文件中的站点和应用程序级别进行配置。

特性

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

指定是否已启用匿名身份验证。

默认值为 true
logonMethod 可选的枚举属性。

logonMethod 属性可以是下列可能值之一。 默认为 ClearText
说明
Batch 此登录类型适用于批处理服务器,无需用户直接干预即可代表用户执行进程。

数值为 1
ClearText 此登录类型保留身份验证包中的姓名和密码,从而允许服务器在模拟客户端时连接到其他网络服务器。

数值为 3
Interactive 此登录类型适用于以交互方式使用计算机的用户。

数值为 0
Network 此登录类型适用于高性能服务器对明文密码进行身份验证。 不会缓存此登录类型的凭据。

数值为 2
password 可选的 String 属性。

指定匿名身份验证的密码。

注意:为避免在配置文件中存储未加密的密码字符串,请始终使用 AppCmd.exe 或 IIS 管理器来输入密码。 如果使用这些管理工具,密码字符串将在写入 XML 配置文件之前自动进行加密。 与存储未加密的密码相比,这种方式提供的密码安全性更高。
username 可选的 String 属性。

指定匿名身份验证的用户名。 如果将此值留空(即 username=""),匿名身份验证将使用应用程序池标识对匿名用户进行身份验证。

默认值为 IUSR

子元素

无。

配置示例

以下配置示例将 IIS 7 网站或 Web 应用程序的匿名身份验证配置为使用 Web 服务器上的本地帐户。 (IIS 7 自动使用 AES 加密来加密密码。)

<security> <authentication>
   <anonymousAuthentication
      userName="User1"
      password="[enc:AesProvider:57686f6120447564652c2049495320526f636b73:enc]" />
   </authentication>
</security>

代码示例

以下示例启用匿名身份验证,将用于匿名身份验证的默认用户名和密码更改为帐户 IUSR 和密码 P@ssw0rd

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/anonymousAuthentication /enabled:"True" /commit:apphost
appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/anonymousAuthentication /userName:"IUSR" /commit:apphost
appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/anonymousAuthentication /password:"P@ssw0rd" /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 anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso");
         anonymousAuthenticationSection["enabled"] = true;
         anonymousAuthenticationSection["userName"] = @"IUSR";
         anonymousAuthenticationSection["password"] = @"P@ssw0rd";
         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 anonymousAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso")
      anonymousAuthenticationSection("enabled") = True
      anonymousAuthenticationSection("userName") = "IUSR"
      anonymousAuthenticationSection("password") = "P@ssw0rd"
      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var anonymousAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/anonymousAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso");
anonymousAuthenticationSection.Properties.Item("enabled").Value = true;
anonymousAuthenticationSection.Properties.Item("userName").Value = "IUSR";
anonymousAuthenticationSection.Properties.Item("password").Value = "P@ssw0rd";

adminManager.CommitChanges();

VBScript

Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set anonymousAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/anonymousAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso")
anonymousAuthenticationSection.Properties.Item("enabled").Value = True
anonymousAuthenticationSection.Properties.Item("userName").Value = "IUSR"
anonymousAuthenticationSection.Properties.Item("password").Value = "P@ssw0rd"

adminManager.CommitChanges()