Friday, May 18, 2007

Request.UserHostAddress and Vista

A common feature I incorporate in my ASP.NET apps is to log the IP address of a visitor. My code worked fine until I upgraded to Windows Vista where my IP logging code failed with the not so friendly error message:

String or binary data would be truncated. The statement has been terminated.

Apparently the Request.UserHostAddress in my code was getting the IP6 address which Vista preferred. I had to do the following which I got from The Cable Guy at Microsoft Technet to make Vista prefer IP4 over IP6:

To selectively disable Pv6 components and configure behaviors for IPv6 in Windows Vista, create and configure the following registry value (DWORD type):
• HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tcpip6\Parameters\DisabledComponents
DisabledComponents is set to 0 by default.
The DisabledComponents registry value is a bit mask that controls the following series of flags, starting with the low order bit (Bit 0):
• Bit 0 Set to 1 to disable all IPv6 tunnel interfaces, including ISATAP, 6to4, and Teredo tunnels. Default value is 0.
• Bit 1 Set to 1 to disable all 6to4-based interfaces. Default value is 0.
• Bit 2 Set to 1 to disable all ISATAP-based interfaces. Default value is 0.
• Bit 3 Set to 1 to disable all Teredo-based interfaces. Default value is 0.
• Bit 4 Set to 1 to disable IPv6 over all non-tunnel interfaces, including LAN interfaces and Point-to-Point Protocol (PPP)-based interfaces. Default value is 0.
• Bit 5 Set to 1 to modify the default prefix policy table to prefer IPv4 to IPv6 when attempting connections. Default value is 0. For more information about the prefix policy table.

To determine the value of DisabledComponents for a specific set of bits, construct a binary number consisting of the bits and their values in their correct position and convert the resulting number to hexadecimal. For example, if you want to disable 6to4 interfaces, disable Teredo interfaces, and prefer IPv4 to IPv6, you would construct the following binary number: 101010. When converted to hexadecimal, the value of DisabledComponents is 0x2A.

You must restart the computer for the changes to the DisabledComponents registry value to take effect.