10

asp.net 中有没有办法限制仅从本地主机访问网页?

4

3 回答 3

13
     if (!HttpContext.Current.Request.IsLocal)
     { 
       Response.Status = "403 Forbidden";
       Response.End();
     }
于 2012-08-10T11:20:41.080 回答
8

如果您想为“网页”执行此操作,那么我会使用 IsLocal,但如果您想要一个子目录解决方案,我会使用 Url Rewrite 2。http ://www.microsoft.com/web/gallery/install。 aspx?appid=urlrewrite2。如果您还没有安装它,请去获取它,因为它非常有用。我相信它将成为 IIS8 的标准。

然后将其添加到您的 web.config 下<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>
于 2012-08-09T14:39:31.097 回答
0

这可能是一个解决方案:

protected void Page_Load(object sender, EventArgs e)
{
    string localhost = Request.Url.Authority;
    if (localhost.IndexOf("localhost") != 0)
        Response.Redirect("defalut.aspx");
}
于 2012-08-09T13:26:36.417 回答