I know this is not your question, but I just felt I needed to point that out. You can run into serious problems reaching public ip addresses if you're using them for internal networking.
Yes I am aware of that ... was just the first example that passed through my head at that time and I do have some network knowledge :) Was just an example (bad one) :)
Ok good deal. I just wanted to make sure you were aware.
As to getting the ip address of the client connecting you should be able to retrieve their IP address like this:
Dim strIP as String = Request.UserHostAddress()
Once you have their IP address in a string you could preform a split on the IP on "." to get an array and then compare each array value to see if it meets your criteria.
So given your criteria above I could this:
Dim ipArray as Array = Split(Request.UserHostAddress(), ".")
'Now Check to make sure each octet is in the correct range:
Dim ipCheck as Boolean = False
If ipArray(0) = 194 and ipArray(1) = 28 and ipArray(2) = 12 and (ipArray(3) > 0 and ipArray(3) < 21) Then
ipCheck = True
End If
Our boolean assumes the host is invalid and only will return as true if all criteria is met.
This will return false for your IP when you run it from Visual Studio because it's going to see your ip as the local loopback address 127.0.0.1.
If you upload this to your server and run it from a client you'll see different results when it's not grabbing your loopback address:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim strIP As String = Request.UserHostAddress()
Response.Write(strIP & ": ")
Response.Write(checkIP(strIP) & "<br />")
Response.Write("194.12.28.1: ")
Response.Write(checkIP("194.28.12.1") & "<br />")
End Sub
Function checkIP(ByVal ip1 As String) As Boolean
Dim ipCheck As Boolean = False
Dim ipArray As Array = Split(ip1, ".")
If ipArray(0) = 194 And ipArray(1) = 28 And ipArray(2) = 12 And (ipArray(3) > 0 And ipArray(3) < 21) Then
ipCheck = True
End If
Return ipCheck
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Marked as answer by Ales.Sm on May 04, 2012 06:19 AM
Ales.Sm
Member
1 Points
11 Posts
Check IP Range
May 02, 2012 02:47 AM|LINK
Hi,
I need to implement a mechanism that will check after login successfull, if the user IP address is within a given range!
For example this range: 194.28.12.1 to 194.28.12.20
I need to check if the current ip address of the user that just loged (for example: 194.28.12.50) is within that range or not?
Thanks!
Loganix77
Participant
1351 Points
412 Posts
Re: Check IP Range
May 02, 2012 03:29 AM|LINK
I know this is not related to your question, but as a network engineer I feel it would be wrong not to mention this.
You are aware that 194.x.x.x is not a private ip address range?
Your reserved private ip address ranges are:
I know this is not your question, but I just felt I needed to point that out. You can run into serious problems reaching public ip addresses if you're using them for internal networking.
Ales.Sm
Member
1 Points
11 Posts
Re: Check IP Range
May 02, 2012 11:59 AM|LINK
Hi,
Yes I am aware of that ... was just the first example that passed through my head at that time and I do have some network knowledge :) Was just an example (bad one) :)
Loganix77
Participant
1351 Points
412 Posts
Re: Check IP Range
May 02, 2012 12:54 PM|LINK
Ok good deal. I just wanted to make sure you were aware.
As to getting the ip address of the client connecting you should be able to retrieve their IP address like this:
Dim strIP as String = Request.UserHostAddress()
Once you have their IP address in a string you could preform a split on the IP on "." to get an array and then compare each array value to see if it meets your criteria.
So given your criteria above I could this:
Dim ipArray as Array = Split(Request.UserHostAddress(), ".")
'Now Check to make sure each octet is in the correct range:
Dim ipCheck as Boolean = False
If ipArray(0) = 194 and ipArray(1) = 28 and ipArray(2) = 12 and (ipArray(3) > 0 and ipArray(3) < 21) Then
ipCheck = True
End If
Our boolean assumes the host is invalid and only will return as true if all criteria is met.
Does that help?
Loganix77
Participant
1351 Points
412 Posts
Re: Check IP Range
May 03, 2012 09:07 PM|LINK
This will return false for your IP when you run it from Visual Studio because it's going to see your ip as the local loopback address 127.0.0.1.
If you upload this to your server and run it from a client you'll see different results when it's not grabbing your loopback address:
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim strIP As String = Request.UserHostAddress() Response.Write(strIP & ": ") Response.Write(checkIP(strIP) & "<br />") Response.Write("194.12.28.1: ") Response.Write(checkIP("194.28.12.1") & "<br />") End Sub Function checkIP(ByVal ip1 As String) As Boolean Dim ipCheck As Boolean = False Dim ipArray As Array = Split(ip1, ".") If ipArray(0) = 194 And ipArray(1) = 28 And ipArray(2) = 12 And (ipArray(3) > 0 And ipArray(3) < 21) Then ipCheck = True End If Return ipCheck End Function </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>