I have a page, on which a request from other website drops in. I want to track IP address where the request is coming.
I am using Asp.Net C# & used three methods
1) httpRequest.UserHostAddress
Tried Http Server variables as
2) httpRequest.ServerVariables ["HTTP_X_FORWARDED_FOR"];
3) httpRequest.ServerVariables ["REMOTE_ADDR"];
But these methods are returning me my server address. As browser is taking this request as it is origonated at my end. But i want to get ip address of the page (Site) where the request is coming from. Can anyone help me in this.
Maybe you're looking for the Url Referrer properties.
Try
Request.UrlReferrer
Or
Request.ServerVariables["http_referer"]
With this you get the Url where the request come from.
private IPAddress[] PossibleReferringIPs
{
get
{
Uri refer = Request.UrlReferrer;
if(refer == null)
return null;
string host = refer.Host;
IPAddress hostAsIP;
if(IPAddress.TryParse(host, out hostAsIP))// had actual IP address as host part of URI
return new IPAddress[]{hostAsIP};
return Dns.GetHostAddresses(host);//This can throw SocketException which you may wish to catch at this point.
}
}
We can't guarantee that the referrer is set at all, even if there was a referrer, and if there is more than one IP address assigned to the domain we cannot know which served the page as since there is no connection between that server and yours, that information isn't available.
Have you tried
httpRequest.UrlReferrer.Host
?
Related
In My application the scenario like i want to read the client requested URL from ASP.NET WEB API.
Example:
https://xxx.test.com/test.html page is calling https://api.test.com/api/home/get/1 WEB method.
The requested url https://xxx.test.com/test.html need to read from the web method.
The below code is returning IP Address. It is not returning domain url.
// GET api/home/get/5
public string Get(int id)
{
return HttpContext.Current.Request.UserHostAddress;
}
Please suggest me.
Thanks in Advance.
Look to HttpContext.Current.Request.UrlReferrer.OriginalString. Note that this data was set by the client, so you can't trust it's 100% accurate.
Try this :
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
If you're behind a load balancer or other reverse proxy, when asking for HttpContext.Current.Request.UserHostAddress, you won't get the IP of the client, instead you'll get the IP of the load balancer. In that case, look to HttpContext.Current.Request.Headers["X-Forwarded-For"] for the browser's IP address. Note that if you aren't behind such hardware, this is a great attack vector.
You can try HttpRequest.UrlReferrer. The MSDN reference can be found here
I am generating run time <a> link. To complete link I am using bellow code:
string appPath = protocol + System.Web.HttpContext.Current.Request.ServerVariables["HTTP_HOST"] + System.Web.HttpContext.Current.Request.ApplicationPath;.
But when user try to open web site from: http://123.123.123.123/testApp at this time my link is created with http://myservername.com/testApp.
I want the address what ever user enter.
If user Open open website from http://123.123.123.123/testApp the link should be
http://123.123.123.123/testApp/Default.aspx
and If User open website from http://myservername.com/testApp the link should be
http://myservername.com/testApp/Default.aspx
Use REMOTE_ADDR server variable
string appPath = protocol +
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] +
System.Web.HttpContext.Current.Request.ApplicationPath;
One of HTTP_HOST or SERVER_NAME should provide you with information on what user typed to get to your site. It may be good idea to check with Http debugger what browser actually sends in case of IP address to make sure you check correct values.
`HttpContext.Current.Request.ServerVariables("HTTP_HOST");`
Use this function to get the IPAddress of the user.
But there is a possibility that you may not get the actual IP. There are many concern like firewall etc. Which may expose same ip for all the computer on the network.
public static string GetMachineName(HttpRequest moRequest)
{
return moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"] != null ||
moRequest.ServerVariables["HTTP_CLIENT_IP"] != null
? moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"]
: moRequest.ServerVariables["REMOTE_ADDR"];
}
In my application i want to check that the url is accessed in the particular ip address. Only one time the page needs to be displayed and for the next time navigating to that page the page should not come.
How can i achieve this?
Regards,
Prasad
I've used this recently to identify users IP-Address(es) to log them on failed logins:
// Look for a proxy address first
var IP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
//Trim and lowercase IP if not null
if (IP != null)
{
IP = IP.ToLower().Trim();
}
if (IP == null || IP.Equals("unknown"))
{
//If IP is null use different detection method else pull the correct IP from list.
IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToLower().Trim();
}
String[] IPs = null;
if (IP.IndexOf(",") > -1)
{
IPs = IP.Split(',');
}
else
{
IPs = new string[] { IP };
}
( note: converted from VB.NET )
Are you actually looking to display the page once per client, or genuinely once per IP?
If it's client machine based, then have that page store a cookie and check for it's presence to determine if the page has been visited before.
You can try storing the IP address in the database or you can make use of a cookie variable. Read more about Cookies
as suggested and described here: How to get a user's client IP address in ASP.NET?
you can check this in the Page_Load
Request.ServerVariables["REMOTE_ADDR"];
and if access was already done once you can use Server.Transfer to move to another page (like a warning page or back to home page etc...)
the way you persist which IP accessed your page once is up to you and your needs, Session, Application Cache, database, cookies...
When the page is loaded get the IP-address and store it in a database. Then, also when the page is loaded, check the IP against a possible entry in the database. If it doesn't exist, store it and load the page. If it aleady exists, redirect the user or something.
I am creating a Http proxy that seats between the web browser and the web server and based on my requirements the proxy server should get the IP address and port number of the web browser that has made a request. Here is a class that represent the connection between the proxy and web browser.
public class Client
{
public Client(IPAddress browserIP, int browserPort)
{
/*Use browserIP and browserPort to create a socket object*/
}
}
Note that i am not using neither HttListener nor HttpRequest objects! I have created a custom Request object that allows me to set the http headers and other stuff that the HttpRequest object doesn't do;but my Request object doesn't have a method to get the browser IP address and Port.
Check out this class
You can use the Request object to get the IP of the requesting end.
string remoteAddr = Request.UserHostAddress;
EDIT: That'll get you the Hostname. good enough to get started with!
string ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
Try the above. it fetches the ip of the requesting client.
How do you get the ipaddress and location of every website vistor of your website through Asp.Net?
Thanks
To get the user's IP use:
Request.UserHostAddress
You can use this webservice to get their geographic location.
http://iplocationtools.com/ip_location_api.php
string VisitorIPAddress = Request.UserHostAddress.ToString();
and based on the ipaddress you can narrow down the location: find the geographical location of a host
Request.UserHostAddress won’t work if you're behind a proxy. Use this code:
public static String GetIPAddress()
{
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
else
ip = ip.Split(',')[0];
return ip;
}
Note that HTTP_X_FORWARDED_FOR should be used BUT as it can return multiple IP addresses separated by a comma you need to use the Split function. See this page for more info.
Well the following property should give you the IP Address of teh client (or the clients proxy server)
Request.UserHostAddress
As for location, you'd need to use some GeoIP/GeoLocation plugin like MaxMind to figure that out.
http://www.maxmind.com/
To get the IP do:
Request.UserHostAddress
And you can map IP to location using a webservice (slower) or a database (faster) like this:
http://ip-to-country.webhosting.info/node/view/5
It's server technology-agnostic, but I'd recommend piggy-backing on Google's AJAX loader: http://code.google.com/apis/ajax/documentation/#ClientLocation
It's in Javascript and will even give you the person's city/state/country (well, it takes a guess based on IP address). Post it back to the server and it's available to you in ASP.NET or whatever.