1
0

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.

Azure Web Apps にデプロイした ASP.NET Core アプリのレスポンスヘッダを制御する

Last updated at Posted at 2020-04-15

Azure Web Apps にデプロイした ASP.NET Core アプリのレスポンスヘッダを制御する

# 以下の記事が書かれた時の版数は .NET Core 3.1 (3.1.201), ASP.NET Core 3.1 (3.1.3) となります.

セキュリティ監査とかでヘッダを制御しないといけない人向け.

まず、そもそも Azure Web Apps ではアプリは IIS 上にホスティングされるので、IIS の設定で制御できる. IIS の設定は当然 web.config です.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By"/>
        <add name="X-Custom-By-Configuration" value="USO800"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

これで X-Powered-By を消しつつ、X-Custom-By-Configuration というフィールドをレスポンスヘッダに追加できます.

コードでやりたい場合には Startup.cs の Configure の中で

app.Use((context, next) =>
{
    context.Response.Headers["X-Custom-By-Code"] = "USO800";
    return next.Invoke();
});

みたいに書けば、X-Custom-By-Code というフィールドをレスポンスヘッダに追加できます.

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?