Web サイトの HSTS 設定 <hsts>

概要

<site> 要素の <hsts> 要素には、IIS 10.0 バージョン 1709 以降のサイトの HTTP Strict Transport Security (HSTS) 設定を構成できる属性が含まれています。

Note

特定のサイトに対して <hsts> セクションと <siteDefaults> セクションの両方で <site> 要素が構成されている場合は、<site> セクションの構成がそのサイトに対して使用されます。

互換性

バージョン メモ
IIS 10.0 バージョン 1709 <site> 要素の <hsts> 要素は IIS 10.0 バージョン 1709 で導入されました。
IIS 10.0 該当なし
IIS 8.5 該当なし
IIS 8.0 該当なし
IIS 7.5 該当なし
IIS 7.0 該当なし
IIS 6.0 該当なし

段取り

<site> 要素の <hsts> 要素は、IIS 10.0 バージョン 1709 以降の既定のインストールに含まれています。

操作方法

IIS 10.0 バージョン 1709 の <site> 要素の <hsts> 要素を構成できるユーザー インターフェイスはありません。 <site> 要素の <hsts> 要素をプログラムで構成する方法の例については、このドキュメントの「サンプル コード」セクションを参照してください。

構成

属性

属性 説明
enabled 省略可能な Boolean 属性です。

サイトの HSTS が有効 (true) か無効 (false) かを指定します。 HSTS が有効になっている場合、IIS が Web サイトに HTTPS 要求を応答するときに Strict-Transport-Security HTTP 応答ヘッダーが追加されます。

既定値は false です。
max-age 省略可能な uint 属性。

Strict-Transport-Security HTTP 応答ヘッダー フィールド値の max-age ディレクティブを指定します。

既定値は 0 です。
includeSubDomains 省略可能な Boolean 属性です。

includeSubDomains ディレクティブを Strict-Transport-Security HTTP 応答ヘッダー フィールド値に含めるかどうかを指定します。

注: すべてのサブドメインが実際に TLS/SSL 経由で HTTP ベースのサービスを提供している場合にのみ、この属性を有効にします。

既定値は false です。
preload 省略可能な Boolean 属性です。

preload ディレクティブを Strict-Transport-Security HTTP 応答ヘッダー フィールド値に含めるかどうかを指定します。

注: HSTS プリロード リストに含めるためにサイトのドメインが送信されている場合にのみ、この属性を有効にします。

既定値は false です。
redirectHttpToHttps 省略可能な Boolean 属性です。

HTTP から HTTPS へのリダイレクトが有効 (true) か無効 (false) かを指定します。

注:redirectHttpToHttps を有効にすると、サイト レベルでの HTTP から HTTPS へのリダイレクトが強制されます。 IIS では HTTP 要求をリダイレクトするとき、URI スキームを "https" に置き換え、ポート コンポーネントを無視します。 リダイレクト先が、標準ポート 443 で TLS/SSL 経由の HTTP ベースのサービスを提供していることを確認します。

既定値は false です。

子要素

なし。

構成サンプル

次の構成サンプルは、HTTP バインドと HTTPS バインドの両方で HSTS が有効になっている Contoso という名前の Web サイトを示しています。 max-age 属性は 31536000 秒 (1 年) に設定されるため、ユーザー エージェントは、Strict-Transport-Security ヘッダー フィールドの受信後 1 年間、ホストを既知の HSTS ホストと見なします。 includeSubDomains 属性は、この HSTS ホスト (contoso.com) とサブドメイン (www.contoso.commarketing.contoso.com など) に HSTS ポリシーが適用されることを指定するために true として設定されます。 最後に、redirectHttpToHttps 属性は、サイトへのすべての HTTP 要求が HTTPS にリダイレクトされるように true として設定されます。

<site name="Contoso" id="1">
    <application path="/" applicationPool="Contoso">
        <virtualDirectory path="/" physicalPath="C:\Contoso\Content" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:80:contoso.com" />
        <binding protocol="https" bindingInformation="*:443:contoso.com" sslFlags="0" />
    </bindings>
    <hsts enabled="true" max-age="31536000" includeSubDomains="true" redirectHttpToHttps="true" />
</site>

サンプル コード

次のコード サンプルでは、HTTP と HTTPS の両方のバインドを持つ Contoso という名前の Web サイトに対して HSTS を有効にします。 このサンプルでは、max-age 属性を 31536000 秒 (1 年) に設定し、includeSubDomains 属性と redirectHttpToHttps 属性の両方を有効にします。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.enabled:True" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.max-age:31536000" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.includeSubDomains:True" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.redirectHttpToHttps:True" /commit:apphost

Note

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 sitesSection = config.GetSection("system.applicationHost/sites");
            ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
            
            ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Contoso");
            if (siteElement == null) throw new InvalidOperationException("Element not found!");

            ConfigurationElement hstsElement = siteElement.GetChildElement("hsts");
            hstsElement["enabled"] = true;
            hstsElement["max-age"] = 31536000;
            hstsElement["includeSubDomains"] = true;
            hstsElement["redirectHttpToHttps"] = true;

            serverManager.CommitChanges();
        }
    }
    
    private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
    {
        foreach (ConfigurationElement element in collection)
        {
            if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
            {
                bool matches = true;
                for (int i = 0; i < keyValues.Length; i += 2)
                {
                    object o = element.GetAttributeValue(keyValues[i]);
                    string value = null;
                    if (o != null)
                    {
                        value = o.ToString();
                    }
    
                    if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
                    {
                        matches = false;
                        break;
                    }
                }
                if (matches)
                {
                    return element;
                }
            }
        }
        return null;
    }
}

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 sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection

      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Contoso")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim hstsElement As ConfigurationElement = siteElement.GetChildElement("hsts")
      hstsElement("enabled") = True
      hstsElement("max-age") = 31536000
      hstsElement("includeSubDomains") = True
      hstsElement("redirectHttpToHttps") = True

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;

var siteElementPos = FindElement(sitesCollection, "site", ["name", "Contoso"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var hstsElement = siteElement.ChildElements.Item("hsts");
hstsElement.Properties.Item("enabled").Value = true;
hstsElement.Properties.Item("max-age").Value = 31536000;
hstsElement.Properties.Item("includeSubDomains").Value = true;
hstsElement.Properties.Item("redirectHttpToHttps").Value = true;

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch)
{
    for (var i = 0; i < collection.Count; i++)
    {
        var element = collection.Item(i);
        if (element.Name == elementTagName)
        {
            var matches = true;
            for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2)
            {
                var property = element.GetPropertyByName(valuesToMatch[iVal]);
                var value = property.Value;
                if (value != null)
                {
                    value = value.toString();
                }
                if (value != valuesToMatch[iVal + 1])
                {
                    matches = false;
                    break;
                }
            }
            if (matches)
            {
                return i;
            }
        }
    }
    
    return -1;
}

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Contoso"))

If siteElementPos = -1 Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If

Set siteElement = sitesCollection.Item(siteElementPos)
Set hstsElement = siteElement.ChildElements.Item("hsts")
hstsElement.Properties.Item("enabled").Value = True
hstsElement.Properties.Item("max-age").Value = 31536000
hstsElement.Properties.Item("includeSubDomains").Value = True
hstsElement.Properties.Item("redirectHttpToHttps").Value = True

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function

IISAdministration PowerShell コマンドレット

Import-Module IISAdministration
Reset-IISServerManager -Confirm:$false
Start-IISCommitDelay

$sitesCollection = Get-IISConfigSection -SectionPath "system.applicationHost/sites" | Get-IISConfigCollection
$siteElement = Get-IISConfigCollectionElement -ConfigCollection $sitesCollection -ConfigAttribute @{"name"="Contoso"}
$hstsElement = Get-IISConfigElement -ConfigElement $siteElement -ChildElementName "hsts"
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "enabled" -AttributeValue $true
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "max-age" -AttributeValue 31536000
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "includeSubDomains" -AttributeValue $true
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "redirectHttpToHttps" -AttributeValue $true

Stop-IISCommitDelay
Remove-Module IISAdministration