Skip Navigation LinksHome > Categories > Code from a Category

Returning an IPv4 Address in an IPv6-Enabled Environment

User Name: jamest85
Name: James Tang
Contact Me:
Home Page:
Google Apps for Work Partner, Certified Zoho CRM Consultant. 7 years experience on Zoho consulting, implementation, and programming using C#, VB, Php..., 15 years of total IT experience including prog... [More]
Viewed Times: 1730
Add Date: 09/25/2012
In this article we will learn how to returning an IPv4 Address in an IPV6-Enabled Environment.
The Internet is best described as a "network of networks," and every device which is connected to the Internet is uniquely identified by its Internet Protocol (IP) address. When client browsers connect to websites, they pass along a collection of information which the website can use for various purposes - these are known as the Request Headers, and the IP address of the client machine is included in this information. Although a machine's IP address is not guaranteed to be 100% accurate, many web developers capture it for logging and other purposes.

One challenge in capturing IP addresses is that there are differences in the IP address format between IP version 4 and IP version 6. When the IPv4 protocol is the only protocol enabled on the web server, accessing the IP address in the Request Headers will return the familiar IPv4 address. However, if IPv6 is also enabled, it will take precedence over IPv4. While earlier versions of Windows supported IPv6, Windows Vista is the first to have it enabled by default. Therefore, if you are using Windows Vista as the operating system for your development machine or web server, you may have noticed that the client IP addresses retrieved from the Request Headers are being returned in the IPv4 format.

In this article we will examine a short bit of code that will seamlessly convert the IPv6 address returned by Windows Vista (or other IPv6-enabled environments) into an IPv4 format. This is useful if your database or back-end logic is expecting an IPv4 address.

An IP Overview
In the early 1980s, version 4 (IPv4) of the Internet Protocol was introduced, and this version is still in use on the vast majority of networks around the world. IPv4 addresses are in the format, e.g. Each of the four elements of an IPv4 address is comprised of a single byte of data, which means that the largest possible IPv4 address is This gives a maximum number of unique IPv4 addresses at just over 4 billion, though approximately 18 million of these are reserved for private networks. Although processes such as Network Address Translation (NAT) have helped expand the potential pool of IP addresses, with more computers, cell phones, and Internet-connect devices, the limits imposed by IPv4 are beginning to be reached. Therefore, version 6 of Internet Protocol was devised in the mid 1990s. Unlike the 32-bit IPv4 addresses, IPv6 addresses are 128-bit in the format: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx, where each x is a 4-bit hexidecimal digit. This expanded format gives a theoretical maximum number of unique IPv6 addresses of 2128 - this is thought highly unlikely ever to run out.

A common method used by ASP.NET developers for retrieving a visitor's IP address from the Request Headers is to use the UserHostAddress property of the HttpRequest object like so:

When the IPv4 protocol is the only protocol enabled, this will return a familiar IPv4 address. However, if IPv6 is also enabled, it will take precedence over IPv4, and the above code will return an IPv6 address instead. Since the release of the Vista Update patch for Visual Studio.NET 2005 SP1, many ASP.NET developers have started to use Vista as their development platform. Although Microsoft shipped the IPv6 protocol with previous versions of Windows, Vista is the first version where IPv6 is enabled by default. This, of course, means that Request.UserHostAddress no longer works as expected because it is suddenly returning an IPv6 address.

Although IPv6 can be disabled in Vista, there is thankfully no need to do this, as it is very easy to return an IPv4 address even when IPv6 is enabled. Firstly, add the following code to your ASP.NET project:

Then use the following code every time you need an IPv4 address:

In this article we saw how to program ASP.NET to return an IPv4 address in an IPv6-enabled environment. As the IPv4 protocol is likely to co-exist with the IPv6 protocol for the foreseeable future, this gives web programmers the best of both worlds.

Happy Programming!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.