2012-08-09 80 views
8

在asp.net中有沒有限制只從本地主機訪問網頁的方法?如何限制只有本地主機的頁面訪問?

+0

你想要什麼,如果非本地主機請求時發生本地主機是如何出現? – freefaller 2012-08-09 08:37:06

+0

限制訪問 – zirus 2012-08-09 08:39:17

+1

是的,我認爲我們理解訪問是受限制的......但是完全**應該發生什麼**? **用戶應該看到什麼**?他們是否被指引到某個地方? (如果你正在回覆一個人,你需要輸入一個'@',然後輸入用戶名,否則他們將不會收到通知) – freefaller 2012-08-09 08:45:22

回答

0

,這可能是一個解決方案:

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

如果你想爲一個「網頁」這樣做,那麼我會用IsLocal,但如果你想我會使用一個子目錄的解決方案URL重寫2 。http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2。如果你還沒有安裝它,那就去做吧,因爲它非常有用。我相信它將成爲IIS8的標準。

那麼這下<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> 
12
 if (!HttpContext.Current.Request.IsLocal) 
    { 
     Response.Status = "403 Forbidden"; 
     Response.End(); 
    } 
0

搶添加到您的web.config中的 'REMOTE_ADDR' 並運行對正則表達式。

Dim remoteAddress As String = Request.ServerVariables("REMOTE_ADDR") 
If Regex.IsMatch(remoteAddress, "(::1|127\.0\.0\.1)") Then 
    //Call originated from localhost, display page.. 
End If 

我要補充::1是,如果您的服務器配置爲IPv6的

相關問題