Compare IP address to a range

Discussion in 'ASP.NET / ASP.NET Core' started by derivativeo, Apr 12, 2006.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I would like to validate users to my site my testing if their IP address is within a given range. How should I go about this? The IP address is given in the dotted format such as 203.51.115.650.

    I have experimented with using strings to compare values and this seems to work, but I'm sure there must be a more robust way to test. For example I used the below code:



    Dim ipadd As String = Request.UserHostAddress


    Dim myIpUpper As String = 'Get upper IP value from Database


    Dim myIpLower As String = 'Get lower IP value from Database


    If ipadd >= myIpLower And ipadd <= myIpUpper Then


    Label1.Text = "registered ok "


    Else


    Label1.Text = "NOT registered"


    End If





    Does the .NET framework provide a method to test whether an IP address is within a range?
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    I don't thnk you can do a straight string comparison.

    You probably will need to split the IP into 4 seperate octet and test them as integer.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page