vb.netiis-7windows-server-2008appcmd

Add IP programmatically to ipv4 and domain restriction


Anybody know how to programmatically add an IP address to "IPv4 and domain restrictions" in IIS7 using vb.net.

i.e I want to exclude some ips or range of IPs from accessing certain folders on my server and allow certain users to access it.

I found those nice articles :

http://www.iis.net/configreference/system.webserver/security/ipsecurity/add

https://serverfault.com/questions/305681/iis-7-address-restrictions-importing-list-to-block-china-and-korea-ips

http://www.tuicool.com/articles/uQru6v


Solution

  • Maybe you can add the restrictions by executing some command line tool.

    (The following information is copy-pasted from Microsoft IIS documentation)
    Configure IPv4 Address and Domain Name Allow Rules (IIS 7) (Original document here)

    To create an allow rule for IPv4 address and domain restriction, use the following syntax:

    appcmd set config /section:ipsecurity /+"[ipaddress=' string ',allowed='true',subnetMask=' string ',domainName=' string ']"
    

    The variable ipaddress string is a single IPv4 address. The variable subnetMask string is a subnet mask. Use the subnet mask in combination with the IPv4 address to identify an address space that applies to this rule. The variable domain name string is a domain name. Typically you would use an IP address or a domain name, but not both in the same rule.

    For example, to create an allow rule for IPv4 address 10.0.0.1, type the following at the command prompt, and then press ENTER:

    appcmd set config /section:ipsecurity /+"[ipaddress='10.0.0.1',allowed='true']"
    

    For more information about Appcmd.exe, see Appcmd.exe (IIS 7).

    Update For executing the command, you can use a code like the following

    Sub MySubName
        Process.Start("C:\Windows\System32\inetsrv\appcmd set config yada-yada-yada...")
    End Sub
    

    I've never used the appcmd command line tool, so you'll need to look at the documentation, and understand how to use it.