Asp.net 如何将页面访问权限限制为仅本地主机?

Asp.net 如何将页面访问权限限制为仅本地主机?,asp.net,security,localhost,Asp.net,Security,Localhost,asp.net中是否有方法限制仅从本地主机访问网页?这可能是一种解决方案: protected void Page_Load(object sender, EventArgs e) { string localhost = Request.Url.Authority; if (localhost.IndexOf("localhost") != 0) Response.Redirect("defalut.aspx"); } 如果您想为“网页”执行此操作,那么我将使

asp.net中是否有方法限制仅从本地主机访问网页?

这可能是一种解决方案:

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

如果您想为“网页”执行此操作,那么我将使用IsLocal,但如果您想要子目录解决方案,我将使用Url重写2。如果你还没有安装这个,那就去买吧,因为它非常有用。我相信这将是IIS8的标准配置

然后将其添加到您的web.config中的



如果发出非本地主机请求,您希望发生什么?是的,我想我们知道访问受到限制。。。但究竟应该发生什么呢?用户应该看到什么?他们应该被引导到某个地方吗?(如果你回复的是个人,你需要在他们的用户名后面加一个
@
,否则他们将不会收到通知)@freefiller当然(关于),你甚至不应该尝试访问此页面,因此它可能是4xx http响应。我正在考虑查看HttpRequest.IsLocal属性。是的,
HttpRequest.IsLocal
对您有用。但是你会想重定向到一个基于404的页面(返回404可能会影响页面的浏览能力——尽管我不是100%确定)
<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>
     if (!HttpContext.Current.Request.IsLocal)
     { 
       Response.Status = "403 Forbidden";
       Response.End();
     }