2012-08-09 13 views
8

Czy jest jakiś sposób w asp.net, aby ograniczyć dostęp do strony tylko z localhost?Jak ograniczyć dostęp do strony tylko do localhost?

+0

Czego chcesz się stać, jeżeli wniosek nie localhost jest wykonany? – freefaller

+0

ograniczenia dostępu – zirus

+1

Tak, myślę, że rozumiemy, że dostęp jest ograniczony ... ale dokładnie ** co ** powinno się stać? ** Co ** powinien zobaczyć użytkownik? Czy są one gdzieś skierowane? (Jeśli odpowiadasz konkretnej osobie, musisz wstawić znak '@ ', a następnie jego nazwę użytkownika, w przeciwnym razie nie otrzymasz powiadomienia) – freefaller

Odpowiedz

0

może to być rozwiązanie:

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

Jeśli chcesz to zrobić na „stronie”, a następnie użyję IsLocal, ale jeśli chcesz rozwiązanie podkatalogu użyję Url Przepisz 2 .http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2. Jeśli nie masz tego już zainstalowanego, idź i pobierz, ponieważ jest bardzo użyteczny. Wierzę, że będzie standardem w IIS8.

Następnie dodać to do pliku web.config pod <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

Grab 'REMOTE_ADDR' i uruchomić go przed regex.

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 

Dodam ::1 to jak będzie localhost pojawiać, jeśli serwer jest skonfigurowany dla protokołu IPv6

Powiązane problemy