Two methods for obtaining the Client IP address in Asp.net

Source: Internet
Author: User

Method 1:

/// <Summary>

/// Obtain the logon IP Address

/// </Summary>

/// <Returns> </returns>

String getip ()

{

// You can use the Proxy Server

String userip = request. servervariables ["http_x_forwarded_for"];

If (userip = NULL | userip = "")

{

// No proxy server. If a proxy server obtains the IP address of the proxy server

Userip = request. servervariables ["remote_addr"];

}

Return userip;

}

Method 2: (this method obtains the IP address of the server)

// Using system. NET is required;

String hostname = DNS. gethostname (); // server user name

Response. Write ("hostname:" + hostname );

IPaddress [] address = DNS. gethostaddresses (hostname); // obtain the Server IP address list. The first IP address is address [0].

Ipendpoint = new ipendpoint (Address [0], 1234 );

Response. Write ("Address:" + ipendpoint. Address. tostring (); // output IP Address: 192.168.0.210, not 127.0.0.1

Response. Write ("port:" + ipendpoint. Port. tostring (); // output 1234

Response. Write ("endpoint:" + ipendpoint. tostring (); // The output is 192.168.0.210: 1234.

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.