Posts o[asp.net] Visitor IP address / Region and other details
Post
Cancel

o[asp.net] Visitor IP address / Region and other details

source - http://www.hakimshabir.blogspot.com/2016/01/how-to-retrieve-visitors-ip-address.html

Any user coming to your website/web application has an IP address. If you want to keep track of user ip address along with other details. I The issue is when they’re behind a proxy . So, here are the code snippets in ASP and .Net that first check for an IP addresses that’s forwarded from behind a proxy, and if there’s none then just get the IP address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public string IpAddress() {
 string strIpAddress;
 strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 if (strIpAddress == null) {
  strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
 }
 return strIpAddress;
}

//Now question is how you retieve all the details like counry, region, region code, latitite and logitute
//Here is the complete code
//for getting all details about user

private string GetVisitor() {
 //client connecting to a web server through an HTTP proxy or load balancer
 String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 if (string.IsNullOrEmpty(ip)) {
  ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
 }
 return ip;
}
private DataTable GetLocation(string strIPAddress) {
 WebRequest rssReq = WebRequest.Create("http://freegeoip.net/xml/" +
  strIPAddress);
 WebProxy px = new WebProxy("http://freegeoip.net/xml/" + strIPAddress, true);
 rssReq.Proxy = px;
 rssReq.Timeout = 2000;
 try {
  WebResponse rep = rssReq.GetResponse();
  XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
  DataSet ds = new DataSet();
  ds.ReadXml(xtr);
  return ds.Tables[0];
 } catch {
  return null;
 }
}
protected void btnGetIpaddress_Click(object sender, EventArgs e) {
 try {
  DataTable dt = new DataTable();
  dt = GetLocation(GetVisitor());
  if (dt.Rows.Count > 0) {
   dvIpaddressdetail.DataSource = dt;
   dvIpaddressdetail.DataBind();
  }
  string ipaddress = dt.Rows[0].Field < string=""> (0);
 } catch (Exception ex) {
  throw ex;
 }
}

origin - http://www.pipiscrew.com/?p=3757 asp-net-visitor-ip-address-region-and-other-details

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags