asp.net get client IP address __.net

Source: Internet
Author: User
Tags httpcontext servervariables

Use Request.ServerVariables ("REMOTE_ADDR") in the ASP to obtain the IP address of the client, but if the client is using a proxy server for access, then the IP address of the proxy server is taken, not the true client IP address.

To obtain a client's real IP address through a proxy server, use Request.ServerVariables ("Http_x_forwarded_for") to read it.

However, it should be noted that not every proxy server can use Request.ServerVariables ("Http_x_forwarded_for") to read the real IP client, and some use this method to read the IP is still the proxy server.

It is also important to note that if the client is not accessed through a proxy server, the value taken with Request.ServerVariables ("Http_x_forwarded_for") will be empty. Therefore, if you want to use this method in your program, you can handle this:
......
Userip = Request.ServerVariables ("Http_x_forwarded_for")
If Userip = "" Then Userip = Request.ServerVariables ("REMOTE_ADDR")
......

Method One
HttpContext.Current.Request.UserHostAddress;

Method Two
httpcontext.current.request.servervariables["REMOTE_ADDR"];

Method Three
String strhostname = System.Net.Dns.GetHostName ();
String clientipaddress = System.Net.Dns.GetHostAddresses (strhostname). GetValue (0). ToString ();

Method IV (ignoring the agent)
httpcontext.current.request.servervariables["Http_x_forwarded_for"];

Client:

Method Five
var ip = ' <!--#echo var= ' remote_addr '--> ';
Alert ("Your IP address is" +IP);

Method VI (ignoring the agent)
function getlocalipaddress ()
{
var obj = null;
var rslt = "";
Try
{
obj = new ActiveXObject ("Rcbdyctl. Setting ");
Rslt = obj. getipaddress;
obj = null;
}
catch (E)
{
//
}

return rslt;
}

MCT Maulik Patel from India offers a service-side solution that is good:

if (context.request.servervariables["Http_via"]!=null)/server, using proxy
{
Get the Real client address
ip=context.request.servervariables["Http_x_forwarded_for"]. ToString (); return real client IP.
}
else//If you do not use a proxy server or do not have IP not using proxy or can ' t getting the client IP
{

Get the address of the service side
ip=context.request.servervariables["REMOTE_ADDR"]. ToString (); While it can ' t get the Client IP, it'll return proxy IP.
}
Some clients will not be sent to us because of "header_access deny" security settings ipfrom:http://hi.baidu.com/snake1964/blog/item/ 055b78cb7bb0f513bf09e6f0.html Personal Application: String GetIP ()
{
String userip = request.servervariables["Http_x_forwarded_for"];
if (Userip = null | | userip = = "")
//{
Userip = request.servervariables["REMOTE_ADDR"]; //}
return userip;
string result = String.Empty; result = httpcontext.current.request.servervariables["http_x_forwarded_for"];
if (string. IsNullOrEmpty (Result))
{
result = httpcontext.current.request.servervariables["REMOTE_ADDR"];
} if (string. IsNullOrEmpty (Result))
{
result = HttpContext.Current.Request.UserHostAddress;
} if (string. IsNullOrEmpty (Result) | | ! IsIP (Result))
{
return "127.0.0.1";
return result; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.