Search code examples
azureiisurl-rewritingweb-configazure-web-app-service

How to block access to sub-domain by IP address


I am using Azure web apps and need to block access to my pre-live environments.

I've attempted using the suggestion made here but it doesn't seem to work: https://learnwithshahriar.wordpress.com/2015/08/06/azure-website-101-restrict-access-on-your-staging-site/

My example:

<rewrite>
  <rules>
    <rule name="Block unauthorized IP to dev site" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^dev-slot.example.com" />
        <add input="{REMOTE_ADDR}" pattern="111.222.333.444" negate="true"/>
      </conditions>
      <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Site is not accessible" />
    </rule>
  </rules>
</rewrite>

It basically does nothing and I've tried various minor alterations to no effect.


Solution

  • Are you looking to simply block the IP address within the web.config? I tested the below out in an Azure Web App and was able to block access.

    <configuration>
       <system.webServer>
          <security>
            <ipSecurity allowUnlisted="true">    
               <clear/> <!-- removes all upstream restrictions -->
               <add ipAddress="83.116.19.53"/>   <!-- block one IP  -->                
               <add ipAddress="83.116.119.0" subnetMask="255.255.255.0"/>   <!--block network 83.116.119.0 to 83.116.119.255-->               
            </ipSecurity>
          </security>
          <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>
    </configuration>
    

    Reference Site: https://www.stokia.com/support/misc/web-config-ip-address-restriction.aspx