Asp. NET 6 ways to get a real client IP address

Source: Internet
Author: User
Tags httpcontext servervariables server port metabase

Request.ServerVariables ("REMOTE_ADDR") to obtain the IP address of the client, but if the client is using a proxy server to access, that is the proxy server IP address, rather than the real client IP address, this article will introduce a workaround, A friend you need can refer to the following

using in ASP

Request.ServerVariables ("REMOTE_ADDR") to obtain the IP address of the client, but if the client is accessed using a proxy server, then the IP address of the proxy server is taken, not the real client IP address. &NBSP

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

However, it is important to note that not every proxy server can use Request.ServerVariables ("Http_x_forwarded_for") to read the client's real IP. Some of the IP that is still being read by this method is 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 do this:  
...  
Userip = Request.ServerVariables ("http_x_forwarded_for")  
If Userip = "Then Userip = Request.ServerVariables (" remote_addr ")  
...

Service side :
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 four (ignoring proxies)
httpcontext.current.request.servervariables["Http_x_forwarded_for"];
Client :
Method Five
var ip = ' <!--#echo var= "REMOTE_ADDR"--";
Alert ("Your IP address is" +IP);
Method six (ignoring proxies)

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;
}


22nd added:
The MCT Maulik Patel from India provides a service-side solution that 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'll return proxy IP.
}


Notes
1. Some agents are not sent to our real IP address
2. Some clients will not send us IP because of "header_access deny" security settings

ServerVariables Variable Description

ServerVariables parameters
Response.Write (Request.ServerVariables ("VarName"))
' VarName is the data to be measured

All_http
All HTTP headers sent by the client, and his results are prefixed with HTTP_.

All_raw
All HTTP headers sent by the client, with the same results as when the client was sent, without a prefix http_

Appl_md_path
The metabase path of the application.

Appl_physical_path
The physical path corresponding to the application metabase path.

Auth_password
When you use Basic authentication mode, the customer enters the password in the Password dialog box.

Auth_type
This is the authentication method that the server uses to validate users when they access a protected script.

Auth_User
The user name for the generation verification.

Cert_cookie
The unique client certificate ID number.

Cert_flag
Client certificate flag, if there is a client certificate, the BIT0 is 0. If the client certificate validation is not valid, Bit1 is set to 1.

Cert_issuer
The Publisher field in the user certificate.

Cert_keysize
The number of bits of the Secure Sockets Layer connection keyword, such as 128.

Cert_secretkeysize
The server verifies the number of bits of the private keyword. such as 1024.

Cert_serialnumber
The Serial Number field for the client certificate.

Cert_server_issuer
Publisher field of the server certificate

Cert_server_subject
The subject field of the server certificate.

Cert_subject
The subject field of the client certificate.

Content_length
The length of the content emitted by the client.

Content_Type
The data type of the form content or HTTP put sent by the customer.

Gateway_interface
The gateway interface used by the server.

HTTPS
Returns on if the request crosses the secure channel (SSL). Returns off if the request is from a non-secure channel.

Https_keysize
The number of bits of the Secure Sockets Layer connection keyword, such as 128.

Https_secretkeysize
The server verifies the number of bits of the private keyword. such as 1024.

Https_server_issuer
The publisher field of the server certificate.

Https_server_subject
The subject field of the server certificate.

instance_id
The ID number of the IIS instance.

Instance_meta_path
Metabase path of the IIS instance that responds to the request.

Local_addr
Returns the address of the server that accepted the request.

Logon_User
User logged on to Windows NT account

Path_info
The path information provided by the client.

Path_translated
The path that is obtained from the virtual to physical mapping.

Query_string
Query string contents.

Remote_addr
The IP address of the remote host that made the request.

Remote_host
The name of the remote host that made the request.

Request_method
The method of making the request. such as, post, and so on.

Script_name
The name of the execution script.

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

Server_port
The server port number on which the request was accepted.

Server_port_secure
If the server port that accepts the request is a secure port, it is 1, otherwise 0.

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

Server_software
The name and version of the server software that answers the request and runs the gateway.

Url
Provide a basic part of the URL

Asp. NET 6 ways to get a real client IP address

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.