简体   繁体   中英

Hi How do I change this applicationHost.xdt to use a custom username/password?

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

I am new to azure. Does anyone know how to add a custom username and password to this config file? the default s user for username and password for password

It is possible to enable Basic Authentication for Azure Web Apps with some settings in the applicationHost.xdt . You can load some modules in this file on the start of your Web App.

Steps:

  • Navigate to your WebApp in the Azure Portal
  • In the left menu, search for the header Development Tools an select Advanced Tools (Kudu)
  • Use the Debug Console > CMD tool , to navigate to the WebApp directory: \home\site
  • Create a file named: applicationHost.xdt
  • Paste the following:
    <?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>
  • Change the Basic Auth to your liking (default in example is: user:password)
  • Make sure the web.config rewrite rules don't contain as this wil remove the effects from the applicationHost.xdt file
  • Save the file and Stop and Start your WebApp (a simple Restart will not suffice)

Notes:

  • Not sure if this works on Linux based WebApps..
  • You can add this step to you're deployment pipelines by using FTP

Update: I've noticed issues with applicationHost.xdt while using it on secondary Web App slots. Only the primary slot seems to work.

For more information you can refer this server fault discussion. You can also refer this SO thread(SO thread is using the devBridge tools to enable basic authentication).

I found the solution for my question:

All I had to do is to run this command in the browser's console with the new username and password:

btoa('newuser:newpassword');

this will return a base64 encoded string like "dXNlcjpwYXNzd29yZA==", then all you have to do is to change it in the xdt file:

<conditions>
    <add input="{HTTP_AUTHORIZATION}" pattern="^Basic <YOUR NEWLY GENERATED STRING>" ignoreCase="false" negate="true" />
</conditions>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM