Using the bellow code .
protected string GetUserIP()
{
string strUserIP = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
strUserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
strUserIP = HttpContext.Current.Request.UserHostAddress;
}
return strUserIP;
}
i get the IPaddress like the format ::1 .
How do I get the correct IP address of a system.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It is of localhost ::1 if you use on web server you will get the correct one.
Though it will depend on the configuration of the network from where the user is accessing your application.
There can be firewall which doesn’t expose the actual IP of the client system.
Method 2
Method that gets IP address: ASP.NET, C#
using System;
using System.Web;
namespace WebApplication1
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Get request.
HttpRequest request = base.Request;
// Get UserHostAddress property.
string address = request.UserHostAddress;
// Write to response.
base.Response.Write(address);
// Done.
base.CompleteRequest();
}
}
}
Method 3
To get the IpAddress use the below code
string IPAddress = GetIPAddress();
public string GetIPAddress()
{
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList) {
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
IPAddress = Convert.ToString(IP);
}
}
return IPAddress;
}
Method 4
Client IP can be read from request:
context.Request.ServerVariables["REMOTE_HOST"]
Here is code for getting more than just client IP address:
string browserInfo =
"RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";n"
+ "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";n"
+ "Type=" + context.Request.Browser.Type + ";n"
+ "Name=" + context.Request.Browser.Browser + ";n"
+ "Version=" + context.Request.Browser.Version + ";n"
+ "MajorVersion=" + context.Request.Browser.MajorVersion + ";n"
+ "MinorVersion=" + context.Request.Browser.MinorVersion + ";n"
+ "Platform=" + context.Request.Browser.Platform + ";n"
+ "SupportsCookies=" + context.Request.Browser.Cookies + ";n"
+ "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";n"
+ "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";n"
+ "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "n";
(or)
string IPAddress = string.Empty; string SearchName = string.Empty; String strHostName = HttpContext.Current.Request.UserHostAddress.ToString(); IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
ServerVariables
Method 5
You can find IP Address in a way given below. It works for me 🙂
If you want only one IP then pick up first IP from the list.
var Dnshost = Dns.GetHostEntry(Dns.GetHostName()); string ipAddress = ""; IPAddress[] ipAddress = Dnshost.AddressList; ipAddress = ipAddress[0].ToString();
Here “ipAddress[0]” will give you current IP of system. And if you want all IP’s to get then iterate the AddressList as shown below:
foreach (var ip in Dnshost.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
}
}
NOTE: It will give you the IPv4 Addresses in case of “AddressFamily.InterNetwork” and if you need IPv6 Addresses you will use “AddressFamily.InterNetworkV6”.
Hope it will be helpful for you.
Method 6
::1 is the IPv6 localhost address, you can find more information here
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0