Ip range how to?

Can somone show how to set an ip range?

Page 1 of 1

1 Replies - 9358 Views - Last Post: 27 May 2005 - 02:34 PM Rate Topic: -----

#1 C#Newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-May 05

Ip range how to?

Posted 27 May 2005 - 01:47 PM

I'm brand new to the board and to C#. I'm trying to put together a directory that displays info based on the ip address. I'm trying to check the remote user's ip address against an ip range.

Can somone show me how to set an ip range in C#? For example, I want to check all ip's between 141.211.161.0 and 141.211.161.254. Here's what I have so far:

if (HttpContext.Current.Request.UserHostAddress != "141.211.161.0 - 141.211.161.254")
{
tbUsers.Columns.Add("Email",System.Type.GetType("System.String"));
}

This is being done in Visual Studio 2003.

Thanks,
Steve

This post has been edited by C#Newbie: 27 May 2005 - 02:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Ip range how to?

#2 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Ip range how to?

Posted 27 May 2005 - 02:34 PM

One option you have is to convert the address and the range boundaries to 32 bit integers and compare them as you would any other number. For example:

// This will give you a 4 element array consisting of the octets in the IP string
String [] addressOctets = HttpContext.Current.Request.UserHostAddress.Split(".".ToCharArray(), 4);

uint addressValue = 0;

// Shift the first octet over 24 bits so that it's in the highest position
// Thus if addressOctets[0] is 0x8D, addressValue will be 0x8D000000
addressValue = (uint)(Convert.ToUInt32(addressOctets[0]) << 24);

// Shift the second over 16 bits and combine it with the previous result
addressValue = addressValue | (uint)(Convert.ToUInt32(addressOctets[1]) << 16);

// As above
addressValue = addressValue | (uint)(Convert.ToUInt32(addressOctets[2]) << 8);

// Don't need to shift the last octet
addressValue = addressValue | Convert.ToUInt32(addressOctets[3]);

// If you can't hard code your boundaries, repeat the above on them.
// Otherwise, you can just convert them by hand:

if (addressValue >= 0x8DD3A100 && addressValue <= 0x8DD3A1FE)
{
  // Address was in the specified range
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1