Asp. NET 6 ways to get IP (reproduced in Lancezhang's Tech Blog)

Source: Internet
Author: User
Tags get ip servervariables

Service side:

1 //Method One2 HttpContext.Current.Request.UserHostAddress;3 4 //Method Two5httpcontext.current.request.servervariables["REMOTE_ADDR"];6 7 //Method Three8 stringStrhostname =System.Net.Dns.GetHostName ();9 stringclientipaddress = System.Net.Dns.GetHostAddresses (strhostname). GetValue (0). ToString ();Ten  One //method Four (ignoring proxies) Ahttpcontext.current.request.servervariables["http_x_forwarded_for"];

Client:

1 //Method Five2 varIP ='<!--#echo var= "REMOTE_ADDR" -';3Alert"Your IP Address is"+IP);4 5 6 7 //method Six (ignoring proxies)8 function getlocalipaddress ()9 { Ten     varobj =NULL;  One     varRSLT ="";  A     Try  -     {  -obj =NewActiveXObject ("Rcbdyctl. Setting");  theRSLT =obj. getipaddress;  -obj =NULL;  -     }  -     Catch(e) +     {  -         //  +     } A     returnrslt; at}

An MCT Maulik Patel from India provides a service-side solution:

1 if(context.request.servervariables["Http_via"]!=NULL)//using proxy2 { 3ip=context.request.servervariables["http_x_forwarded_for"]. ToString ();//Return Real client IP.4 }5 Else//Not using proxy or can ' t get the Client IP6 { 7ip=context.request.servervariables["REMOTE_ADDR"]. ToString ();//while it can ' t get the Client IP, it'll return proxy IP.9}

Note:

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

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.