10

localhost からのみ Web ページへのアクセスを制限する方法が asp.net にありますか?

4

3 に答える 3

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

「Web ページ」に対してこれを行いたい場合は 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 に答える