How to restrict access to the page only to the local host?

Is there a way in asp.net to restrict access to a webpage only with localhost?

+8
security localhost
source share
4 answers
if (!HttpContext.Current.Request.IsLocal) { Response.Status = "403 Forbidden"; Response.End(); } 
+12
source share

If you want to do this for a "web page", then I would use IsLocal, but if you want to use a subdirectory, I would use Url Rewrite 2. http://www.microsoft.com/web/gallery/install.aspx? appid = urlrewrite2 . If you don’t have it yet, go and get it as very useful. I believe that it will be standard for IIS8.

Then add this to your web.config under <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> 
+6
source share

this might be a solution:

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

Take "REMOTE_ADDR" and run it against 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 

I have to add ::1 , as localhost will display if your server is configured for IPv6

0
source share

All Articles