Working in ASP/VBSCRIPT, I have a snippet that denies access to a page if
the user's IP does not belong to the local network:
<%
Dim RemoteAddr
RemoteAddr = Request.ServerVariables("REMOTE_ADDR")
If instr(RemoteAddr, "192.168.0") Then
Else Response.Redirect ("/Login/entry_denied.asp")
End If
%>
I need to apply this to a ASPX page. Can you help me with the syntax?
David Wier - 07 Jun 2007 18:31 GMT
Just put this logic at the very first of the Page_Load routine, in your ASPX
page
change Dim RemoteAddr to:
Dim RemoteAddr as String
remove the <% %> tags and you're good to go

Signature
David Wier
MVP/ASPInsider
http://aspnet101.com
http://iWritePro.com
> Working in ASP/VBSCRIPT, I have a snippet that denies access to a page if
> the user's IP does not belong to the local network:
[quoted text clipped - 8 lines]
>
> I need to apply this to a ASPX page. Can you help me with the syntax?
Juan T. Llibre - 07 Jun 2007 18:46 GMT
There's also an extraneous "Else"...
This should cover it, if placed in the Page_Load event :
Dim RemoteAddr as String = Request.ServerVariables("REMOTE_ADDR")
If instr(RemoteAddr, "192.168.0") Then
Response.Redirect ("/Login/entry_denied.asp")
End If
Mark's code is simpler, though.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
> Just put this logic at the very first of the Page_Load routine, in your ASPX page
> change Dim RemoteAddr to:
[quoted text clipped - 13 lines]
>>
>> I need to apply this to a ASPX page. Can you help me with the syntax?
Billy Bob - 07 Jun 2007 19:01 GMT
My mistake on the original code, should be:
Dim RemoteAddr as String = Request.ServerVariables("REMOTE_ADDR")
If instr(RemoteAddr, "192.168.0") <> 0 Then
Response.Redirect ("/Login/entry_denied.asp")
End If
> There's also an extraneous "Else"...
>
[quoted text clipped - 29 lines]
>>>
>>> I need to apply this to a ASPX page. Can you help me with the syntax?
Juan T. Llibre - 07 Jun 2007 19:20 GMT
Yup. It *was* "extraneous".
:-)
Did you try Mark's code ? That should work.
Since you're using VB.NET, and not C#,
which he used for the sample, here it is in VB.NET :
Private Sub Page_Load(sender As Object, e As System.EventArgs)
If Not Request.ServerVariables("REMOTE_ADDR").ToString().StartsWith("192.168.0") Then
Response.Redirect("/Login/entry_denied.asp", False)
End If
End Sub
Try it...
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
> My mistake on the original code, should be:
>
[quoted text clipped - 35 lines]
>>>>
>>>> I need to apply this to a ASPX page. Can you help me with the syntax?
Billy Bob - 07 Jun 2007 18:59 GMT
I get the error: Only Content controls are allowed directly in a content
page that contains Content controls.
for the line Dim RemoteAddr as String
I am way out of my comfort zone.... anything else?
Bill
> Just put this logic at the very first of the Page_Load routine, in your
> ASPX page
[quoted text clipped - 14 lines]
>>
>> I need to apply this to a ASPX page. Can you help me with the syntax?
Mark Rae - 07 Jun 2007 19:54 GMT
>I get the error: Only Content controls are allowed directly in a content
>page that contains Content controls.
> for the line Dim RemoteAddr as String
1) Are you using MasterPages...?
2) Are you writing your server-side code in-line or in separate code
files...?

Signature
http://www.markrae.net
Billy Bob - 07 Jun 2007 22:26 GMT
>>I get the error: Only Content controls are allowed directly in a content
>>page that contains Content controls.
[quoted text clipped - 4 lines]
> 2) Are you writing your server-side code in-line or in separate code
> files...?
1) MasterPages, yes.
2) ServerSide code is in a separate file, filename.aspx.cs
Thank you for your time. Apparently I didn't provide enough information with
my first request.
Mark Rae - 07 Jun 2007 22:37 GMT
>>>I get the error: Only Content controls are allowed directly in a content
>>>page that contains Content controls.
[quoted text clipped - 7 lines]
> 1) MasterPages, yes.
> 2) ServerSide code is in a separate file, filename.aspx.cs
In which case, if you just want the IP restriction to apply to certain
content pages, put the code in the Page_Load event behind the individual
content page(s).
Or, if you want it to apply to all content pages, put it in the MasterPage's
Page_Load event - however, if your entry_denied page is also a content page,
you'll get stuck in a loop... :-)
If you want it to apply to the entire site, follow John's suggestion.

Signature
http://www.markrae.net
Billy Bob - 08 Jun 2007 00:43 GMT
Mark Rae - 07 Jun 2007 18:35 GMT
> Working in ASP/VBSCRIPT, I have a snippet that denies access to a page if
> the user's IP does not belong to the local network:
[quoted text clipped - 8 lines]
>
> I need to apply this to a ASPX page. Can you help me with the syntax?
private void Page_Load(object sender, System.EventArgs e)
{
if
(!Request.ServerVariables["REMOTE_ADDR"].ToString().StartsWith("192.168.0"))
{
Response.Redirect ("/Login/entry_denied.asp", false);
}
}

Signature
http://www.markrae.net
John Timney (MVP) - 07 Jun 2007 21:22 GMT
Lots of good sugegstions, if it were me I would create an httpmodule that
checks the IPs in the beginRequest event. That way it applies to all pages
and you dont have to put it anywhere else. Code it once, and use it
whereever
http://www.devx.com/dotnet/Article/6962/0/page/1
Regards
John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
> Working in ASP/VBSCRIPT, I have a snippet that denies access to a page if
> the user's IP does not belong to the local network:
[quoted text clipped - 8 lines]
>
> I need to apply this to a ASPX page. Can you help me with the syntax?