在 Azure 的應用程式服務上設定基本驗證

在 Azure 的應用程式服務上設定基本驗證

由於不同的原因,我使用它Azure's App Service來提供靜態文件。我想確保此訪問的安全,Http Basic Authentication這足以滿足我的目的。我怎樣才能做到這一點?我嘗試上傳,.htpasswd但似乎不起作用。

我沒有使用 ASP.NET,所以無法用程式碼來實作。在 Azure 的入口網站中,我在應用程式服務 -> 身份驗證/授權下看到 Google、Facebook、Twitter 登入等選項,但這對我來說是巨大的開銷。

答案1

目前,這是不可能的。 Azure Web 應用程式不支援此功能。

你可以檢查這個回饋

答案2

可以透過下列設定為 Azure Web Apps 啟用基本驗證:應用程式主機.xdt。您可以在 Web 應用程式啟動時載入此文件中的一些模組。

腳步:

  • 導覽至 Azure 入口網站中的 WebApp
  • 在左側選單中,搜尋標題開發工具一個選擇進階工具(捻角羚)
  • 使用偵錯控制台 > CMD工具,導航到 WebApp 目錄:\主頁\網站
  • 建立一個名為:應用程式主機.xdt
  • 貼上以下內容:
<?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 dXNlcjpwYXNzd29yZA==" ignoreCase="false" negate="true" />
            </conditions>
            <action type="CustomResponse" statusCode="401" statusReason="Unauthorized" statusDescription="Unauthorized" />
            <serverVariables>
              <set name="RESPONSE_WWW_AUTHENTICATE" value="Basic realm=Project" />
            </serverVariables>
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
  </location>
</configuration>
  • 根據您的喜好變更基本驗證(範例中的預設值為:使用者:密碼)
  • 確保 web.config 重寫規則不包含,<clear />因為這會消除 applicationHost.xdt 檔案的影響
  • 儲存檔案並停止並啟動您的 WebApp(一個簡單的重新開始將要不是足夠了)

筆記:

  • 不確定這是否適用於基於 Linux 的 WebApps..
  • 您可以使用 FTP 將此步驟新增至您的部署管道中
  • 更新:我在輔助 Web 應用程式插槽上使用 applicationHost.xdt 時注意到它有問題。似乎只有主插槽可以工作。

答案3

相關內容