2012-08-09 21 views
8

¿Hay alguna manera en asp.net de limitar el acceso a una página web solo desde localhost?¿Cómo limitar el acceso a la página solo a localhost?

+0

¿Qué desea que suceda si la solicitud no se hace localhost? – freefaller

+0

restringir el acceso – zirus

+1

Sí, creo que entendemos que el acceso está restringido ... pero exactamente ** qué ** debería suceder? ** ¿Qué ** debería ver el usuario? ¿Deberían ser dirigidos a alguna parte? (Si está respondiendo a un individuo, debe poner un '@' seguido de su nombre de usuario, de lo contrario no recibirán una notificación) – freefaller

Respuesta

0

esto podría ser una solución:

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

Si quieres hacer esto por una "página web", entonces me gustaría usar IsLocal, pero si quieres una solución subdirectorio que haría uso de reescritura de URL 2 http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2. Si aún no lo tiene instalado, vaya a buscarlo ya que es muy útil. Creo que será estándar en IIS8.

A continuación, agregue esto a su web.config bajo <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

Coge la 'REMOTE_ADDR' y correr contra una expresión regular.

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 

debo añadir ::1 es como localhost aparecerá si el servidor está configurado para IPv6

Cuestiones relacionadas