1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

powershell で X-AspNet-Version ヘッダを削除する

Posted at

サーバ全体で無効化する方法がないか調べてみたところ msdn ブログがヒットしました。

Remove Unwanted HTTP Response Headers

方法は 2 種類あって、web.config に追加する方法と、サーバレベルでサーバ変数を利用した rewrite を行う方法があるようです。
ただし、後者は ヘッダそのものではなく、ヘッダの値を隠蔽 するというものになっています。

web.config での方法

以下の 3 行をトップレベルの web.config に追記します。

  <system.web>
      <httpRuntime enableVersionHeader="false" />
  </system.web>

または下記コマンドを実行します。

c:\windows\system32\inetsrv\appcmd.exe set config -section:system.web/httpRuntime -enableVersionHeader:false /commit:webroot /clr:4

.NET2 の場合は /clr:2 とします。
すると ROOT である C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Web.config に上記内容が追加されます。

IISフォーラム先生ありがとうございます。
System.Web in Applicationhost.config not working anymore (IIS8.5)

applicationHost.config での方法

IIS 8 からは applicationHost.config での system.web セクションは廃止されたとのこと。

rewrite での方法

Please note that it will not remove the header all together but it will remove the value of it.

あまり意味はなさそうですが、とりあえずやってみようということで、サイト内の手順に沿って IIS マネージャから設定したところ、 applicationHost.config に設定が追加されてしまいました。

    <system.webServer>
        <rewrite>
            <allowedServerVariables>
                <add name="RESPONSE_X-ASPNET-VERSION" />
            </allowedServerVariables>
            <outboundRules>
                <rule name="RESPONSE_X-ASPNET-VERSION">
                    <match serverVariable="RESPONSE_X-ASPNET-VERSION" pattern=".+" />
                    <action type="Rewrite" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>

powershell でできないか調べてみたところ、いくつかのコマンドレットを使うとよさそうです。

Get-WebConfigurationProperty
Add-WebConfigurationProperty
Set-WebConfiguration

スクリプトは以下のようになりました。

# add allowedServerVariables
$asv = Get-WebConfigurationProperty `
       -filter "//rewrite/allowedServerVariables/add"`
       -name name

if ($asv -eq $null)
{
  Add-WebConfigurationProperty `
    -Filter "//rewrite/allowedServerVariables" `
    -PSPath "IIS:\" `
    -Name "Collection" `
    -Value @{name="RESPONSE_X-ASPNET-VERSION"}
}

# add rule
$rule = Get-WebConfigurationProperty `
          -filter "//rewrite/outboundRules/rule[@name='REMOVE_X-ASPNET-VERSION']/match"`
          -name serverVariable

if ($rule -eq $null)
{
  Add-WebConfigurationProperty `
    -Filter "//rewrite/outboundRules" `
    -PSPath "IIS:\" `
    -Name "Collection" `
    -Value @{name="REMOVE_X-ASPNET-VERSION"}

  Set-WebConfiguration `
    -Filter "//rewrite/outboundRules/rule[@name='REMOVE_X-ASPNET-VERSION']/match" `
    -PSPath "IIS:\" `
    -Value @{serverVariable="RESPONSE_X-ASPNET-VERSION";pattern=".+"}
  
  Set-WebConfiguration `
    -Filter "//rewrite/outboundRules/rule[@name='REMOVE_X-ASPNET-VERSION']/action" `
    -PSPath "IIS:\" `
    -Value @{type="Rewrite"}
}

画像のようにバージョンが空になります。

Screen Shot 2015-10-27 at 16.19.21.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?