在 Azure 的应用服务上配置基本身份验证

Tom*_*ski 7 azure http-basic-authentication

由于不同的原因,我Azure's App Service用来提供静态文件。我想确保这种访问Http Basic Authentication足以满足我的目的。我怎样才能做到这一点?我尝试上传,.htpasswd但似乎不起作用。

我没有使用 ASP.NET,所以没有办法在代码中做到这一点。在 Azure 的门户中,我在 App Service -> Authentication/Authorization 下看到了 Google、Facebook、Twitter 登录等选项,但这对我来说是巨大的开销。

The*_*tch 7

可以使用applicationHost.xdt中的某些设置为 Azure Web Apps 启用基本身份验证。您可以在启动 Web 应用程序时加载此文件中的一些模块。

脚步:

  • 导航到 Azure 门户中的 WebApp
  • 在左侧菜单中,搜索标题“开发工具”并选择“高级工具(Kudu)”
  • 使用调试控制台 > CMD工具导航到 WebApp 目录:\home\site
  • 创建一个名为:applicationHost.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>
Run Code Online (Sandbox Code Playgroud)
  • 根据您的喜好更改基本身份验证(示例中的默认值为:用户:密码)
  • 确保 web.config 重写规则不包含<clear />,因为这会消除 applicationHost.xdt 文件的影响
  • 保存文件并停止并启动您的 WebApp(简单的重新启动不够的)

笔记:

  • 不确定这是否适用于基于 Linux 的 WebApps..
  • 您可以使用 FTP 将此步骤添加到您的部署管道中
  • 更新:我在辅助 Web 应用程序插槽上使用 applicationHost.xdt 时注意到它存在问题。似乎只有主插槽可以工作。


Shu*_*bao 2

目前,这是不可能的。Azure Web 应用程序不支持此功能。

您可以查看此反馈