【问题标题】:How to limit page access only to localhost?如何将页面访问仅限于本地主机?
【发布时间】:2012-08-06 10:44:18
【问题描述】:

asp.net 中有没有办法限制只能从 localhost 访问网页?

【问题讨论】:

  • 如果发出非本地主机请求,您希望发生什么?
  • 是的,我认为我们理解访问受到限制...但究竟应该发生什么用户应该看到什么?他们会被引导到某个地方吗? (如果您要回复个人,则需要在其用户名后加上@,否则他们将不会收到通知)
  • @freefaller 确定(关于@),甚至不应该尝试访问此页面,因此它可能是一些 4xx http 响应。我正在考虑查看 HttpRequest.IsLocal 属性。
  • 是的,HttpRequest.IsLocal 会为您工作。但是您会希望重定向到基于 404 的页面(返回 404 可能会开始影响页面被查看的能力 - 尽管我对此不是 100% 确定)

标签: asp.net security localhost


【解决方案1】:
     if (!HttpContext.Current.Request.IsLocal)
     { 
       Response.Status = "403 Forbidden";
       Response.End();
     }

【讨论】:

    【解决方案2】:

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

    然后将其添加到<system.webServer/> 下的 web.config 中

    <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>
    

    【讨论】:

      【解决方案3】:

      这可能是一个解决方案:

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

      【讨论】:

        猜你喜欢
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多