I have provided a couple of updates to this code in light of the comments. Thanks for the input!
Code Snippet
- // Invoker
- string IPAddress = IPHelper.GetIPAddress(Request.ServerVariables["HTTP_VIA"],
- Request.ServerVariables["HTTP_X_FORWARDED_FOR"],
- Request.ServerVariables["REMOTE_ADDR"]);
- public class IPHelper
- {
- /// <summary>
- /// Gets the user's IP Address
- /// </summary>
- /// <param name="httpVia">HTTP_VIA Server variable</param>
- /// <param name="httpXForwardedFor">HTTP_X_FORWARDED Server variable</param>
- /// <param name="RemoteAddr">REMOTE_ADDR Server variable</param>
- /// <returns>user's IP Address</returns>
- public static string GetIPAddress(string HttpVia, string HttpXForwardedFor, string RemoteAddr)
- {
- // Use a default address if all else fails.
- string result = "127.0.0.1";
- // Web user - if using proxy
- string tempIP = string.Empty;
- if (HttpVia != null)
- tempIP = HttpXForwardedFor;
- else // Web user - not using proxy or can't get the Client IP
- tempIP = RemoteAddr;
- // If we can't get a V4 IP from the above, try host address list for internal users.
- if (!IsIPV4(tempIP) || tempIP == "127.0.0.1 ")
- {
- try
- {
- string hostName = System.Net.Dns.GetHostName();
- foreach (System.Net.IPAddress ip in System.Net.Dns.GetHostAddresses(hostName))
- {
- if (IsIPV4(ip))
- {
- result = ip.ToString();
- break;
- }
- }
- }
- catch { }
- }
- else
- {
- result = tempIP;
- }
- return result;
- }
- /// <summary>
- /// Determines weather an IP Address is V4
- /// </summary>
- /// <param name="input">input string</param>
- /// <returns>Is IPV4 True or False</returns>
- private static bool IsIPV4(string input)
- {
- bool result = false;
- System.Net.IPAddress address = null;
- if (System.Net.IPAddress.TryParse(input, out address))
- result = IsIPV4(address);
- return result;
- }
- /// <summary>
- /// Determines weather an IP Address is V4
- /// </summary>
- /// <param name="address">input IP address</param>
- /// <returns>Is IPV4 True or False</returns>
- private static bool IsIPV4(System.Net.IPAddress address)
- {
- bool result = false;
- switch (address.AddressFamily)
- {
- case System.Net.Sockets.AddressFamily.InterNetwork: // we have IPv4
- result = true;
- break;
- case System.Net.Sockets.AddressFamily.InterNetworkV6: // we have IPv6
- break;
- default:
- break;
- }
- return result;
- }
- }
End of Code Snippet
8 comments:
Thanks, thanks and thanks!! Great piece of code, weeks looking for something like this!
Thanks a lot. Very helpful, readable code. Thaks a lot again and again.
Thanks man for sharing your knowledge and helping out the little people :)
This was exactly what I was looking for ! Thnx
I made one small adaptation in my code for the case where you/debug run the application on localhost. Then the REMOTE_ADDR is the loopback ip 127.0.0.1 which is correct but not what we are looking for. So also in that case I look up the ip via the hostname adpting the code as follows :
// If we can't get a V4 IP from the above, try host address list for internal users.
if (!IsIPV4(tempIP) || tempIP=="127.0.0.1")
Is it me that just don´t understand the code, but you are never returning the value in the tempIP variable. So if you have an IP address that is not an IPv6, you will always get the default of 127.0.0.1 returned.
Thanks for the comments guys, I've provided an update to the code in light of these comments!
Amazing knowledge of .Net, thanks for sharing this code. I was stuck on this problem!.... totally solved! Thanks.
what is my ipv4
Post a Comment