Reputation: 17
How do I get a user's IP address with .NET code? I'm using Foo Basic Web Studio IDE that uses ASP.net code.
Upvotes: 1
Views: 259
Reputation: 14216
Here is the way you can that IP address of users.
string clientIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if(string.IsNullOrEmpty(clientIp))
clientIp = Request.ServerVariables("REMOTE_ADDR")
Upvotes: 0
Reputation: 1418
Here is another way of doing it:
' Get your Hostname
Dim szHost As String = System.Net.Dns.GetHostName()
' Get the host entry for said hostname
Dim hostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(szHost)
' To get the ip address of the first available net interface:
Dim ip As System.Net.IPAddress = New System.Net.IPAddress(hostEntry.AddressList(0).GetAddressBytes)
Note that hostEntry
contains the AddressList
array, listing the IP-Addresses of all active network interfaces.
So you might want to check/filter them for relevance.
Upvotes: 0
Reputation: 1544
string strHostName = HttpContext.Current.Request.UserHostAddress.ToString();
string IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
Upvotes: 1