programming.torensma.net: Code Snippets

HttpContext.Current.Request.UserHostAddress returns ::1

One would expect HttpContext.Current.Request.UserHostAddress to return the IP address of the client computer. On my developement machine this would be 127.0.0.1. The application I’m working on acts on this IP address, so it is fital to get the correct address.

Since I’ve moved to Windows Server 2008 x64 on my laptop, I got ::1 as a result from HttpContext.Current.Request.UserHostAddress. After some searching I found out that the hosts file actually contained two entries for localhost: 127.0.0.1 and ::1. Removing the ::1 entry solved the problem. I’ve heard people using Windows Vista also have this problem.

Update sept. 2009:

After installing Windows 7 I had the exact same problem again. I opened up my hosts file, and was very surprised to find an entry

#  ::1  localhost

The starting # actually comments out this line! I removed the # (thus enabling ::1) and now my app works as expected. Looks like Windows Vista and Windows 7 switched the way they handle this IP resolving…

Update July 2010:

After a fresh Windows 7 64-bit installation I again ran into the above problem, which I thought I could fix easily. But not so this time. After every Visual Studio built the problem kept coming back. Somehow IIS keeps sending its IP6 address to the client. Long story short, after uncommenting #127.0.0.1 in the hosts file everything works like a charm.

You can follow any responses to this entry through the RSS 2.0 feed.