Request.UserHostAddress, Vista & IIS7

M

Mark Rae

Hi,

Now that the VS.NET 2005 SP1 update patch is with us, I'm in the process of
moving my main development environment onto 64-bit Vista Business Edition -
so far, so good...

However, there is a bit of a gotcha with Request.UserHostAddress...

Under IIS6 and earlier, that would return a standard xxx.xxx.xxx.xxx IP
address. However, Vista enables IP6 by default, which makes
Request.UserHostAddress return an IP6 address e.g.
"fe80::2032:39ab:3f57:fffb%10"

Easy enough to disable IP6, of course, but does anyone know a way to return
an IP4 address from IIS7 without disabling IP6, or mucking about with
LMHOSTS...

Haven't been able to find a way so far...
http://west-wind.com/WebLog/posts/8839.aspx
http://forums.asp.net/thread/1667538.aspx

Any assistance gratefully received.

Mark
 
J

Juan T. Llibre

This article shows you how to set Vista to prefer IPv4 to IPv6 when attempting connections:

http://www.microsoft.com/technet/community/columns/cableguy/cg1005.mspx

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.

So, you'd set the first 4 bits to 0 and bit 5 to 1.

That will allow IPv6, but the system will prefer IPv4.

To only prefer IPv4 over IPv6 (what you want), set the value to 0x20
To disable IPv6 over all interfaces *and* prefer IPv4 to IPv6, set it to 0xFF

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

Mark Rae

This article shows you how to set Vista to prefer IPv4 to IPv6 when
attempting connections:

http://www.microsoft.com/technet/community/columns/cableguy/cg1005.mspx

I think I've found a way to return the IP4 address even when IP6 is on...

Dns.GetHostAddresses(Dns.GetHostName()) returns an array if IP addresses for
the current host and, so far, the IP4 address always seems to be the fourth
element of the array i.e.

[0]: {fe80::288f:1d8f:3f57:fffb%10}
[1]: {fe80::7133:8615:3971:49d1%8}
[2]: {fe80::5efe:192.168.0.4%13}
[3]: {192.168.0.4}
[4]: {2001:0:4136:e38e:288f:1d8f:3f57:fffb}

Therefore:

using System.Net;
string strIP4Address =
Dns.GetHostAddresses(Dns.GetHostName())[3].ToString();

I need to do a bit more testing to see what that returns on XP / Win2k3s.

Also, it's almost certainly an extremely bad idea to rely on the numerical
position of the element within the array...
 
M

Mark Rae

I need to do a bit more testing to see what that returns on XP / Win2k3s.

And here we go:

using System.Net;

public string GetIP4Address()
{
string strIP4Address = String.Empty;

foreach (IPAddress objIP in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (objIP.AddressFamily.ToString() == "InterNetwork")
{
strIP4Address = objIP.ToString();
break;
}
}
return strIP4Address;
}

See here for further details:
http://msdn2.microsoft.com/en-us/library/system.net.ipaddress.addressfamily.aspx
 
J

Juan T. Llibre

Good job, Mark!




Mark Rae said:
And here we go:

using System.Net;

public string GetIP4Address()
{
string strIP4Address = String.Empty;

foreach (IPAddress objIP in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (objIP.AddressFamily.ToString() == "InterNetwork")
{
strIP4Address = objIP.ToString();
break;
}
}
return strIP4Address;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top