Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've tried as below but it's returning ::1 as the IP Address after Hosting the Application. But here when the user requests for the website i want to know whether the user requested to that particular IP or not. So I wanna retrieve the server IPV4 address.

What I have tried:

C#
private String GetIPAddress()   
{
IPServer = string.Empty;   
         IPServer=Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) != null ? Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) : string.Empty;

if (string.IsNullOrEmpty(IPServer))
{
IPServer = Request.ServerVariables["REMOTE_ADDR"];
}                      
return IPServer;
}
Posted
Updated 26-Jun-16 19:39pm
v2

1 solution

The server IP address is static - or it should be - meaning it never changes. If it does, then the DNS lookup that converts the URL "www.MyDomain.com" to an IP address would not find the site.
It's complicated, but when the user requests a URL from the browser, the following things happen:
The browser asks the OS for server IP address.
The OS does a DNS lookup and returns the IP address to the browser.
The browser creates a TCP connection to server, and sends an HHTP request via the TCP connection.
The browser receives HTTP response and process it (authorization errors, 404's and suchlike are handled here)
The browser renders response as a page.

All of this uses the IP address, not the URL once the DNS lookup is complete.

You can get the server IP address, that's simple: the FillInDetails method shown here will do it from a server as easily as from a client: Using IP based Geolocation - and why it's pretty much useless.[^]
But it won't change!
 
Share this answer
 
Comments
JanardhanSharma 27-Jun-16 8:11am    
The methods in the below page resolved the issue.

https://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=vs.110).aspx

Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900