2012-08-09 81 views
8

在asp.net中有没有限制只从本地主机访问网页的方法?如何限制只有本地主机的页面访问?

+0

你想要什么,如果非本地主机请求时发生本地主机是如何出现? – freefaller 2012-08-09 08:37:06

+0

限制访问 – zirus 2012-08-09 08:39:17

+1

是的,我认为我们理解访问是受限制的......但是完全**应该发生什么**? **用户应该看到什么**?他们是否被指引到某个地方? (如果你正在回复一个人,你需要输入一个'@',然后输入用户名,否则他们将不会收到通知) – freefaller 2012-08-09 08:45:22

回答

0

,这可能是一个解决方案:

protected void Page_Load(object sender, EventArgs e) 
{ 
    string localhost = Request.Url.Authority; 
    if (localhost.IndexOf("localhost") != 0) 
     Response.Redirect("defalut.aspx"); 
} 
6

如果你想为一个“网页”这样做,那么我会用IsLocal,但如果你想我会使用一个子目录的解决方案URL重写2 。http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2。如果你还没有安装它,那就去做吧,因为它非常有用。我相信它将成为IIS8的标准。

那么这下<system.webServer/>

<rewrite> 
<rules> 
    <!-- if this rule matches stopProcessing any further rules --> 
    <rule name="Block Remote Access to Admin" stopProcessing="true" patternSyntax="ECMAScript" enabled="true"> 
     <!-- specify secure folder matching trailing/or $ == end of string--> 
     <match url="projects(/|$)" ignoreCase="true" /> 
     <conditions logicalGrouping="MatchAll"> 
     <!-- Allow local host --> 
     <add input="{REMOTE_ADDR}" pattern="localhost" ignoreCase="true" negate="true" /> 
     <add input="{REMOTE_ADDR}" pattern="127.0.0.1" negate="true" /> 
     <add input="{REMOTE_ADDR}" pattern="::1" negate="true" /> 
     </conditions> 
     <!-- by default, deny all requests. Options here are "AbortRequest" (drop connection), "Redirect" to a 403 page, "CustomResponse", etc. --> 
     <action type="CustomResponse" statusCode="403" statusDescription="Forbidden" statusReason="Access to this URL is restricted"/> 
     <!-- or send the caller to an error page, home page etc 
      <action type="Redirect" url="/public/forbidden.htm" redirectType="Temporary" /> 
     --> 
    </rule> 
    <rules> 
</rewrite> 
12
 if (!HttpContext.Current.Request.IsLocal) 
    { 
     Response.Status = "403 Forbidden"; 
     Response.End(); 
    } 
0

抢添加到您的web.config中的 'REMOTE_ADDR' 并运行对正则表达式。

Dim remoteAddress As String = Request.ServerVariables("REMOTE_ADDR") 
If Regex.IsMatch(remoteAddress, "(::1|127\.0\.0\.1)") Then 
    //Call originated from localhost, display page.. 
End If 

我要补充::1是,如果您的服务器配置为IPv6的

相关问题