ASP. NET 6 methods for obtaining real client IP addresses

Source: Internet
Author: User
Tags servervariables

Use in ASP

Request. ServerVariables ("REMOTE_ADDR") to obtain the IP address of the client. However, if the client is accessed by a proxy server, the IP address of the proxy server is obtained instead of the real IP address of the client.

To obtain the real IP address of the client through the proxy server, use Request. ServerVariables ("HTTP_X_FORWARDED_FOR") to read.

However, not every proxy server can use Request. serverVariables ("HTTP_X_FORWARDED_FOR") to read the real IP address of the client, some of which are still the IP address of the proxy server.

Note that if the client is not accessed through the proxy server, the value obtained using Request. ServerVariables ("HTTP_X_FORWARDED_FOR") will be null. Therefore, if you want to use this method in the program, you can do this:
......
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then userip = Request. ServerVariables ("REMOTE_ADDR ")
......

Server:
// Method 1
HttpContext. Current. Request. UserHostAddress;
// Method 2
HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];
// Method 3
String strHostName = System. Net. Dns. GetHostName ();
String clientIPAddress = System. Net. Dns. GetHostAddresses (strHostName). GetValue (0). ToString ();
// Method 4 (ignore proxy)
HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"];
Client:
// Method 5
Var ip = '<! -- # Echo var = "REMOTE_ADDR" --> ';
Alert ("Your IP address is" + ip );
// Method 6 (ignore proxy)
Copy codeThe Code is as follows:
Function GetLocalIPAddress ()
{
Var obj = null;
Var rslt = "";
Try
{
Obj = new ActiveXObject ("rcbdyctl. Setting ");
Rslt = obj. GetIPAddress;
Obj = null;
}
Catch (e)
{
//
}
Return rslt;
}

Add on 22nd:
MCT Maulik Patel from India provides a server-side solution, which is good:
Copy codeThe Code is as follows:
If (Context. Request. ServerVariables ["HTTP_VIA"]! = Null) // using proxy
{
Ip = Context. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]. ToString (); // Return real client IP.
}
Else // not using proxy or can't get the Client IP
{
Ip = Context. Request. ServerVariables ["REMOTE_ADDR"]. ToString (); // While it can't get the Client IP, it will return proxy IP.
}

Remarks:
1. Some proxies will not send us real IP addresses.
2. Some clients will not send us IP addresses because of the "header_access deny" security settings.

ServerVariables variable description

ServerVariables Parameters
Response. write (request. serverVariables ("varName "))
'Varname is the data to be tested.

ALL_HTTP
All HTTP headers sent by the client have the prefix HTTP _.

ALL_RAW
All HTTP headers sent by the client have the same results as those sent by the client without the prefix HTTP _

APPL_MD_PATH
The metadatabase path of the application.

APPL_PHYSICAL_PATH
The physical path corresponding to the application metadatabase path.

AUTH_PASSWORD
The password entered in the Password dialog box when the Basic Authentication mode is used.

AUTH_TYPE
When a user accesses a protected script, the server is used to verify the user's authentication method.

AUTH_USER
The authenticated user name.

CERT_COOKIE
The unique ID of the customer certificate.

CERT_FLAG
Customer certificate flag. If there is a client certificate, bit0 is 0. If the client certificate verification is invalid, bit1 is set to 1.

CERT_ISSUER
The issuer field in the user certificate.

CERT_KEYSIZE
Number of digits of the Secure Socket Layer connection keyword, such as 128.

CERT_SECRETKEYSIZE
The number of digits that the server verifies for a private keyword. For example, 1024.

CERT_SERIALNUMBER
The serial number field of the customer certificate.

CERT_SERVER_ISSUER
Issuer field of the server certificate

CERT_SERVER_SUBJECT
The topic field of the server certificate.

CERT_SUBJECT
The topic field of the client certificate.

CONTENT_LENGTH
The length of the content sent by the client.

CONTENT_TYPE
The form Content or http put data type sent by the customer.

GATEWAY_INTERFACE
The gateway interface used by the server.

HTTPS
If the request passes through the Secure Channel (SSL), ON is returned. If the request comes from an insecure channel, return OFF.

HTTPS_KEYSIZE
Number of digits of the Secure Socket Layer connection keyword, such as 128.

HTTPS_SECRETKEYSIZE
The number of digits that the server verifies for a private keyword. For example, 1024.

HTTPS_SERVER_ISSUER
The issuer field of the server certificate.

HTTPS_SERVER_SUBJECT
The topic field of the server certificate.

INSTANCE_ID
The ID of the IIS instance.

INSTANCE_META_PATH
The metabase path of the IIS instance that responds to the request.

LOCAL_ADDR
Returns the server address that receives the request.

LOGON_USER
The user logs on to the Windows NT account

PATH_INFO
The path provided by the client.

PATH_TRANSLATED
The Path obtained by ing from virtual to physical.

QUERY_STRING
Query string content.

REMOTE_ADDR
The IP address of the remote host that sends the request.

REMOTE_HOST
The name of the remote host that sends the request.

REQUEST_METHOD
Request Method. Such as GET, HEAD, and POST.

SCRIPT_NAME
Name of the script to be executed.

SERVER_NAME
The host name, DNS address, or IP address of the server.

SERVER_PORT
The Port Number of the server that receives the request.

SERVER_PORT_SECURE
If the server port receiving the request is a security port, the value is 1. Otherwise, the value is 0.

SERVER_PROTOCOL
The name and version of the protocol used by the server.

SERVER_SOFTWARE
Name and version of the server software that responds to the request and runs the gateway.

URL
Provide the basic part of the URL

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.