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

我是天蓝色的新手。有谁知道如何在这个配置文件中添加自定义用户名和密码?用户名的默认 s 用户和密码的密码

4

2 回答 2

0

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

脚步:

  • 在 Azure 门户中导航到您的 WebApp
  • 在左侧菜单中,搜索标题Development Tools并选择Advanced Tools (Kudu)
  • 使用Debug Console > CMD tool导航到 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>
  • 根据您的喜好更改基本身份验证(示例中的默认值为:用户:密码)
  • 确保不包含 web.config 重写规则,因为这会从 applicationHost.xdt 文件中删除效果
  • 保存文件并停止和启动您的 WebApp(简单的重启是不够的)

笔记:

  • 不确定这是否适用于基于 Linux 的 WebApps..
  • 您可以使用 FTP 将此步骤添加到您的部署管道中

更新:我注意到在辅助 Web 应用程序插槽上使用 applicationHost.xdt 时存在问题。只有主插槽似乎工作。

有关更多信息,您可以参考服务器故障讨论。您也可以参考这个SO线程(SO 线程正在使用 devBridge 工具来启用基本身份验证)。

于 2021-12-02T07:24:35.867 回答
0

我找到了我的问题的解决方案:

我所要做的就是在浏览器的控制台中使用新的用户名和密码运行这个命令:

btoa('newuser:newpassword');

这将返回一个 base64 编码的字符串,如“dXNlcjpwYXNzd29yZA==”,那么您所要做的就是在 xdt 文件中更改它:

<conditions>
    <add input="{HTTP_AUTHORIZATION}" pattern="^Basic <YOUR NEWLY GENERATED STRING>" ignoreCase="false" negate="true" />
</conditions>
于 2021-12-03T10:44:42.127 回答