Sil*_*Fox 13
if (!HttpContext.Current.Request.IsLocal)
{
Response.Status = "403 Forbidden";
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
如果您想为"网页"执行此操作,那么我将使用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>
Run Code Online (Sandbox Code Playgroud)
这可能是一个解决方案:
protected void Page_Load(object sender, EventArgs e)
{
string localhost = Request.Url.Authority;
if (localhost.IndexOf("localhost") != 0)
Response.Redirect("defalut.aspx");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4660 次 |
| 最近记录: |