|
|
|
|
|
今天看到一篇文章,說是 ASP.NET 站點可以在 Web.config 配置 ipSecurity 來拒絕某IP(段)訪問網(wǎng)站,于是躍躍欲試,自己也折騰一下。
此前我已經(jīng)成功在IIS里設(shè)置拒絕某IP(段)訪問網(wǎng)站了,參考文章:
然而事情并不順利,我添加代碼后,返回了一個錯誤提示:500 - 內(nèi)部服務(wù)器錯誤。我使用的代碼是(示例):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<ipSecurity>
<clear/>
<add ipAddress="192.168.100.1"/>
</ipSecurity>
</security>
</system.webServer>
</configuration>
經(jīng)查詢了解到需要修改一下配置文件,操作如下。
打開 applicationHost.config 文件(位于):
找到 <sectionGroup name="system.webServer"> 這個節(jié)點,然后把此節(jié)點內(nèi)的下面這行:
- <section name="ipSecurity" overrideModeDefault="Deny" />
改為:
- <section name="ipSecurity" overrideModeDefault="Allow" />
保存文件。
這樣,代碼就能運行正常了。
設(shè)置后,被拒絕IP訪問網(wǎng)站時就返回 403 - 禁止訪問:訪問被拒絕 的提示。
配置示例
以下配置示例為默認(rèn)網(wǎng)站添加了兩個 IP 限制;第一個限制拒絕訪問 IP 地址 192.168.100.1,第二個限制拒絕訪問整個 169.254.0.0 網(wǎng)絡(luò)。
<system.webServer>
<security>
<ipSecurity>
<add ipAddress="192.168.100.1" />
<add ipAddress="169.254.0.0" subnetMask="255.255.0.0" />
</ipSecurity>
</security>
</system.webServer>
相關(guān)文章