2013-09-29 88 views
0

我已经创建了新的ASP.NET网站。我正在Visual Studio中编辑(Open WebSite和通过FTP)。我想阻止所有人查看网站,除了我的IP地址。只允许一个IP可以访问ASP.NET网站

在PHP中我是这样做在.htaccess这样的:

RewriteEngine On 
RewriteBase/

RewriteCond %{REMOTE_ADDR} !^XX\.XXX\.XXX\.XX$ //my IP 

RewriteCond %{REQUEST_URI} !^/construct 

RewriteRule (.*) /construct/index.php [R=307,L] //redirect when other IP access website 

在ASP.NET我只找到这一解决方案(在Web.config中):

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
     <customErrors mode="Off"/> 
    </system.web> 
    <system.webServer> 
    <security> 
     <ipSecurity allowUnlisted="false"> 
     <clear/> 
     <add ipAddress="XXX.XXX.X.X" allowed="true"/> 
     </ipSecurity> 
    </security> 
    </system.webServer> 
</configuration> 

但是这并未不适合我。当我在写入这个页面后输入页面时,它给了我以下错误:

服务请求时出错。 服务器没有返回任何可以显示的网站。 问题的可能原因是在生成此网页的脚本中。

什么问题或者我该怎么解决这个问题?

+0

您的应用程序是asp.net还是php? –

+0

你有没有在服务器上看过[这个清单](http://www.iis.net/configreference/system.webserver/security/ipsecurity)?假设你有,你可以验证你的客户端(你的机器)使用的IP是否与你配置的限制匹配? – EdSF

+0

在asp.net中,它并不在我自己的服务器上运行,但我已付费托管,因此无法配置服务器。 – BblackK

回答

2

试试这个任何一个存储一个字符串变量

string ip=HttpContext.Current.Request.UserHostAddress.ToString(); 
or 
string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); 
or 
string ip=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 

//然后检查IP地址匹配。如果属实,则允许访问您的代码

if(ip=="xxx.xxx.xx.x")//Your IP address 
{ 
//your code is here 

} 
+1

我已经解决了这个问题,在masterpage上添加了这个内容,并且在特殊页面上重定向了除ip地址以外的所有内容。如果有人不能配置服务器,我认为这是很好的临时解决方案。感谢这个想法。 – BblackK

相关问题