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

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

由于不同的原因,我使用它Azure's App Service来提供静态文件。我想通过Http Basic Authentication这种方式来保护此访问权限,这足以满足我的目的。我该怎么做?我尝试上传,.htpasswd但似乎不起作用。

我没有使用 ASP.NET,所以无法在代码中执行此操作。在 Azure 的门户中,我在应用服务 -> 身份验证/授权下看到了 Google、Facebook、Twitter 登录等选项,但这对我来说是巨大的开销。

答案1

目前还不可能。Azure webapp 不支持此功能。

你可以检查一下反馈

答案2

可以使用以下设置启用 Azure Web 应用的基本身份验证:应用程序主机.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 文件中删除效果
  • 保存文件并停止并启动您的 Web 应用程序(一个简单的重新开始将要不是足夠了)

笔记:

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

答案3

相关内容