Get Client's IP Address in ASP.Net Web Application using C#.Net
A few days ago, I got this bizarre requirement of fetching the IP Address of the Client's PC which was running my Web Application. Though All the PCs were in local Intranet, but It was really challenging and I was feeling it to be a kind of hack.. I finally solved the issue and would like the share the same piece of code snippet.
Code for .NET 4 and Above:
/// <summary> /// method to get Client ip address /// </summary> /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param> /// <returns></returns> public static string GetVisitorIPAddress(bool GetLan = false) { string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (String.IsNullOrEmpty(visitorIPAddress)) visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(visitorIPAddress)) visitorIPAddress = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1") { GetLan = true; visitorIPAddress = string.Empty; } if (GetLan) { if (string.IsNullOrEmpty(visitorIPAddress)) { //This is for Local(LAN) Connected ID Address string stringHostName = Dns.GetHostName(); //Get Ip Host Entry IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName); //Get Ip Address From The Ip Host Entry Address List IPAddress[] arrIpAddress = ipHostEntries.AddressList; try { visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString(); } catch { try { visitorIPAddress = arrIpAddress[0].ToString(); } catch { try { arrIpAddress = Dns.GetHostAddresses(stringHostName); visitorIPAddress = arrIpAddress[0].ToString(); } catch { visitorIPAddress = "127.0.0.1"; } } } } } return visitorIPAddress; }
Code for .NET 3.5 and Below:
/// <summary> /// method to get Client ip address /// </summary> /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param> /// <returns></returns> public static string GetVisitorIPAddress(bool GetLan) { string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (String.IsNullOrEmpty(visitorIPAddress)) visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(visitorIPAddress)) visitorIPAddress = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1") { GetLan = true; visitorIPAddress = string.Empty; } if (GetLan) { if (string.IsNullOrEmpty(visitorIPAddress)) { //This is for Local(LAN) Connected ID Address string stringHostName = Dns.GetHostName(); //Get Ip Host Entry IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName); //Get Ip Address From The Ip Host Entry Address List IPAddress[] arrIpAddress = ipHostEntries.AddressList; try { visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString(); } catch { try { visitorIPAddress = arrIpAddress[0].ToString(); } catch { try { arrIpAddress = Dns.GetHostAddresses(stringHostName); visitorIPAddress = arrIpAddress[0].ToString(); } catch { visitorIPAddress = "127.0.0.1"; } } } } } return visitorIPAddress; }