Getting ipaddress and location of every user visiting your website - c#

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.

Related

Set Allowable IP in Web.Config

I want to set IP in web.config in an MVC project that only an specified ip can visit the site, but other ip's see under construction view.
Can anyone tell me how can i do this?
Thanks.
You have to use HTTP_X_FORWARDED_FOR and REMOTE_ADDR ServerVariables to get Client IP Address then match with your permitted list and perform further action based on your condition.
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}

Get User's IP Address in a Share Point Web Part not giving valid IP address

This code in a standard custom WebPart:
private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
is returning: "fe80::c564:7922:d873:5cf5%11" instead of a valid IP address. It does this for every method I've found on Google for retrieving it, including HttpRequest.UserHostAddress.
Does anyone have an idea what is going on?
Edit: For some reason it's giving me IPv6 when loading the page locally, but works as intended when I access from a different machine :/
Use this,
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Or use
string ipAddress = Request.UserHostAddress

Unable to Get Ip address of the http request : Asp.Net [C#]

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
?

asp.net c# ip address lost after postback?

One of my functions in a class is called GetIpAddress() which returns the following string:
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
This all works well in regular page loads and gets my ip address, but when i for example let a user place a comment, then the ip address is lost after postback and i get an empty string returned.
Am I missing something here maybe?
Kind regards,
Mark
Use these ones:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; // a user is going through a proxy server
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
HttpContext.Current.Request.UserHostAddress;
Some explanation:
REMOTE_ADDR The IP address of the remote host making the request.
REMOTE_HOST The name of the host making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.
Do you have it wrapped in something like this?
if (!Page.IsPostBack) {
GetIpAddress()
}
You need to either call the function every time, or store it in a Shared variable.
Another option (As #Marc mentioned) would be to simply call Request.UserHostAddress instead of GetIpAddress... it's almost the same amount of typing, but you don't have to have a custom function for it.

Determine Client's Computer Name

I am building an intranet site that will display different lists based on the computer name because different computers are in different areas, is there a way (within a controller or model) to determine the client's computer name?
I have tried system.environment.machinename but that only returns the name of the server, any other ideas?
I got it working using the following:
string IP = Request.UserHostName;
string compName = CompNameHelper.DetermineCompName(IP);
code from compnamehelper:
public static string DetermineCompName(string IP)
{
IPAddress myIP = IPAddress.Parse(IP);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
return compName.First();
}
code in VB :
Dim myIP As IPAddress = IPAddress.Parse(Request.UserHostName)
Dim GetIPHost As IPHostEntry = Dns.GetHostEntry(myIP)
Dim compName As List(Of String) = GetIPHost.HostName.ToString.Split("").ToList
return(compName.First)
No. The client's computer name is not available in any way on the server. This is the nature of the http request-response. You only can have its IP address.
A workarounds could be to retrieve machine on the client from Flash/Silverlight (I doubt JavaScript) and put in into cookie which is available on the server with each request. But there is a whole stack of issues with this approach.
I think you are better off using one of these methods to tie a user to a location:
a cookie that is set once the user self-selects their location
having the user login to the site so that you can track them uniquely that way
remembering user by IP address
There is no way of ensuring remote hostnames are unique. The same issue occurs with IP because of proxies, dynamic IP, etc., but I think it will be a little more reliable. Also, you can do geolocation by IP address.
Try this:
string name = Request.UserHostName;
The only way I know of to inspect the client is through the ServerVariables collection on the Request object (should be available for MVC code).
See https://web.archive.org/web/20210927201638/http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. REMOTE_HOST and REMOTE_ADDR look like candidates.
Here's an IE-only solution. It works in IE8, with multiple security warnings.
<script type="text/javascript" language="javascript">
var ax = new ActiveXObject("WScript.Network");
document.write(ax.UserName + '<br />'); //logged in account name
document.write(ax.ComputerName + '<br />'); //Windows PC name
</script>

Categories

Resources