Azure Web App でベーシック認証
2025年7月6日 14時20分手順
- App Service に移動
- 高度なツール > 移動
- Debug console > CMD
- site をクリック(D:\home\site)に移動
- applicationHost.xdt を追加(+ボタン > New file)
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location path="%XDT_SITENAME%" xdt:Locator="Match(path)">
<system.webServer>
<rewrite xdt:Transform="InsertIfMissing">
<allowedServerVariables xdt:Transform="InsertIfMissing">
<add name="RESPONSE_WWW_AUTHENTICATE" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" />
</allowedServerVariables>
<rules xdt:Transform="InsertIfMissing">
<rule name="BasicAuthentication" stopProcessing="true" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)">
<match url=".*" />
<conditions>
<add input="{HTTP_AUTHORIZATION}" pattern="^Basic dXNlcjpob2dlaG9nZQ==" ignoreCase="false" negate="true" />
</conditions>
<action type="CustomResponse" statusCode="401" statusReason="Unauthorized" statusDescription="Unauthorized" />
<serverVariables>
<set name="RESPONSE_WWW_AUTHENTICATE" value="Basic realm=Active" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
dXNlcjpob2dlaG9nZQ== は ユーザー名:パスワード を base64 でエンコードしたもの。
この例では user:hogehoge
- Web App を再起動
2023-07-14 追記(web.configの編集)
2023-07-14 現在では上記の手順では Basic 認証できなくなった。
/home/site/wwwroot/ 配下の web.config に 以下の <rewrite> タグ を追加する方法で Basic 認証を設定できる。
2025-01-08 時点
上記の手順で Basic 認証可能
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\ym2.Web.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
<rewrite>
<rules>
<rule name="BasicAuthOK" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_Authorization}" pattern="Basic dXNlcjpob2dlaG9nZQ==" />
</conditions>
<action type="None" />
</rule>
<rule name="BasicAuthNG" stopProcessing="true">
<match url="^admin/.*$" />
<serverVariables>
<set name="RESPONSE_WWW-Authenticate" value="Basic Realm=SecretZone" replace="true"/>
</serverVariables>
<action type="CustomResponse" statusCode="401" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
参考
https://serverfault.com/questions/901508/configure-basic-authentication-on-azures-app-service
App Service にて Basic 認証機能の実装や設定をする