Request Object

J

Joey

asp.net 2/C#/VS2005

Does anyone know why "Request.UserIPAddress" returns an empty IPV6
address when I run (both serve and browse) my asp.net app on Windows
Vista? It all works fine on Windows XP Pro: a regular IPV4 address is
returned.

How can I force the code to always use the IPV4 address, even on
Windows Vista?

JP
 
M

Mark Rae

asp.net 2/C#/VS2005

Does anyone know why "Request.UserIPAddress" returns an empty IPV6
address when I run (both serve and browse) my asp.net app on Windows
Vista? It all works fine on Windows XP Pro: a regular IPV4 address is
returned.

It will do - Vista enables IPv6 by default, which Request.UserIPAddress will
use rather than IPv4.
How can I force the code to always use the IPV4 address, even on
Windows Vista?

Two ways:

1) Turn IPv6 off

2) Use the function below instead of Request.UserIPAddress

using System.Net;

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

foreach (IPAddress objIP in
Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
{
if (objIP.AddressFamily.ToString() == "InterNetwork")
{
strIP4Address = objIP.ToString();
break;
}
}
if (strIP4Address != String.Empty)
{
return strIP4Address;
}
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

Top