Asp. NET get real client IP Address 6 ways _ Practical Tips

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

using in ASP

Request.ServerVariables ("REMOTE_ADDR") 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")
......

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 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)
Copy Code code 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 add:
MCT Maulik Patel from India offers a service-side solution that is good:
Copy Code code 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 that needs to be measured

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

All_raw
All HTTP headers sent by the client have the same result as the client sent, no 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
The password that the customer enters in the Password dialog box when using the Basic authentication mode.

Auth_type
This is the authentication method that the server uses to verify the user when they access the protected script.

Auth_User
The user name to authenticate.

Cert_cookie
The unique customer Certificate ID number.

Cert_flag
Customer Certificate Flag, if there is a client certificate, then Bit0 is 0. If the client certificate validation is invalid, the bit1 is set to 1.

Cert_issuer
The Publisher field in the user certificate.

Cert_keysize
The number of digits of the Secure Sockets Layer connection key, such as 128.

Cert_secretkeysize
The number of digits that the server validates private keywords. such as 1024.

Cert_serialnumber
The Serial Number field of the client certificate.

Cert_server_issuer
Publisher field for 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 that the client emits.

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

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 unsecured channel.

Https_keysize
The number of digits of the Secure Sockets Layer connection key, such as 128.

Https_secretkeysize
The number of digits that the server validates private keywords. such as 1024.

Https_server_issuer
Publisher field of the server certificate.

Https_server_subject
The subject field of the server certificate.

instance_id
ID number of the IIS instance.

Instance_meta_path
The metabase path in response to the requested IIS instance.

Local_addr
Returns the server address that accepts the request.

Logon_User
User login to Windows NT account

Path_info
The path information provided by the client.

Path_translated
A path that is obtained by a virtual to physical mapping.

Query_string
The contents of the query string.

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

Remote_host
Name of the remote host that issued 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 that accepts the request.

Server_port_secure
1 if the server port that accepts the request is a secure port, or 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 answered the request and ran the gateway.

Url
Provides the basic part of a 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.