Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 03, 2012 09:07 PM by Loganix77
May 02, 2012 02:47 AM|LINK
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: 220.127.116.11 to 18.104.22.168
I need to check if the current ip address of the user that just loged (for example: 22.214.171.124) is within that range or not?
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.
May 02, 2012 11:59 AM|LINK
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) :)
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
Our boolean assumes the host is invalid and only will return as true if all criteria is met.
Does that help?
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">
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(checkIP("126.96.36.199") & "<br />")
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
<form id="form1" runat="server">