Check IP Rangehttp://forums.asp.net/t/1799018.aspx/1?Check+IP+RangeThu, 03 May 2012 21:07:56 -040017990184960721http://forums.asp.net/p/1799018/4960721.aspx/1?Check+IP+RangeCheck IP Range <p>Hi,</p> <p>I need to implement a mechanism that will check after login successfull, if the user IP address is within a given range!</p> <p>For example this range: 194.28.12.1 to 194.28.12.20</p> <p>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?</p> <p>Thanks!</p> 2012-05-02T02:47:51-04:004960751http://forums.asp.net/p/1799018/4960751.aspx/1?Re+Check+IP+RangeRe: Check IP Range <p>I know this is not related to your question, but as a network engineer I feel it would be wrong not to mention this.</p> <p>You are aware that 194.x.x.x is not a private ip address range?</p> <p>Your reserved private ip address ranges are:</p> <table class="wikitable"> <tbody> <tr> <th>RFC1918 name</th> <th>IP address range</th> <th>number of addresses per network</th> <th><i><a title="Classful network" href="/wiki/Classful_network">classful</a></i> description</th> <th>largest <a title="Classless Inter-Domain Routing" href="/wiki/Classless_Inter-Domain_Routing"> CIDR</a> block (subnet mask)</th> <th>host id size</th> </tr> <tr> <td>24-bit block</td> <td>10.0.0.0 10.255.255.255</td> <td>16,777,214</td> <td>single <a class="mw-redirect" title="Class A network" href="/wiki/Class_A_network"> class A</a></td> <td>10.0.0.0/8 (255.0.0.0)</td> <td>24 bits</td> </tr> <tr> <td>20-bit block</td> <td>172.16.0.0 172.31.255.255</td> <td>65,534</td> <td>16 contiguous class Bs</td> <td>172.16.0.0/12 (255.240.0.0)</td> <td>20 bits</td> </tr> <tr> <td>16-bit block</td> <td>192.168.0.0 192.168.255.255</td> <td>254</td> <td>256 contiguous class Cs</td> <td>192.168.0.0/16 (255.255.0.0)</td> <td>16 bits</td> </tr> </tbody> </table> <p>I know this is not your question, but I just felt I needed to point that out.&nbsp; You can run into serious problems reaching public ip addresses if you're using them for internal networking.</p> 2012-05-02T03:29:51-04:004961772http://forums.asp.net/p/1799018/4961772.aspx/1?Re+Check+IP+RangeRe: Check IP Range <p>Hi,</p> <p>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) :)</p> 2012-05-02T11:59:52-04:004961903http://forums.asp.net/p/1799018/4961903.aspx/1?Re+Check+IP+RangeRe: Check IP Range <p>Ok good deal.&nbsp; I just wanted to make sure you were aware.</p> <p>As to getting the ip address of the client connecting you should be able to retrieve their IP address like this:</p> <p>Dim strIP as String = Request.UserHostAddress()</p> <p>Once you have their IP address in a string you could preform a split on the IP on &quot;.&quot; to get an array and then compare each array value to see if it meets your criteria.</p> <p>So given your criteria above I could this:</p> <p>&nbsp;Dim ipArray as Array = Split(Request.UserHostAddress(), &quot;.&quot;)</p> <p>'Now Check to make sure each octet is in the correct range:</p> <p>Dim ipCheck as Boolean = False</p> <p>If ipArray(0) = 194 and ipArray(1) = 28 and ipArray(2) = 12 and (ipArray(3) &gt; 0 and ipArray(3) &lt; 21) Then</p> <p>ipCheck&nbsp;= True</p> <p>End If</p> <p>Our boolean assumes the host is invalid and only will return as true if all criteria is met.</p> <p>Does that help?</p> 2012-05-02T12:54:52-04:004964458http://forums.asp.net/p/1799018/4964458.aspx/1?Re+Check+IP+RangeRe: Check IP Range <p>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.</p> <p>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:</p> <pre class="prettyprint">&lt;%@ Page Language=&quot;VB&quot; %&gt; &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;script runat=&quot;server&quot;&gt; Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim strIP As String = Request.UserHostAddress() Response.Write(strIP &amp; &quot;: &quot;) Response.Write(checkIP(strIP) &amp; &quot;&lt;br /&gt;&quot;) Response.Write(&quot;194.12.28.1: &quot;) Response.Write(checkIP(&quot;194.28.12.1&quot;) &amp; &quot;&lt;br /&gt;&quot;) End Sub Function checkIP(ByVal ip1 As String) As Boolean Dim ipCheck As Boolean = False Dim ipArray As Array = Split(ip1, &quot;.&quot;) If ipArray(0) = 194 And ipArray(1) = 28 And ipArray(2) = 12 And (ipArray(3) &gt; 0 And ipArray(3) &lt; 21) Then ipCheck = True End If Return ipCheck End Function &lt;/script&gt; &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p></p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-05-03T21:07:56-04:00