ASP. NET obtains the Client IP address, user name, and other information.

Source: Internet
Author: User
1. Special attributes in ASP. NET:
Get server computer name: page. server. manchinename
Get user information: page. User
Obtain the client computer name: page. Request. userhostname
Obtain the Client IP Address: page. Request. userhostaddress

2. General methods in network programming:
Get the current computer name: static system. net. DNS. gethostname ()
Retrieve all IP addresses based on the computer name: static system. net. DNS. Resolve (computer name). Addresslist
You can also obtain the computer name static system. net. DNS. Resolve (IP address). hostname Based on the IP address.

3. General attributes of the system environment:
Current computer name: static system. environment. machinename
Domain Name of the current computer: static system. environment. userdomainname
Current computer user: static system. environment. Username

4. If (request. servervariables ["http_via"]! = NULL) {string user_ip = request. servervariables ["http_x_forwarded_for"]. tostring ();
} Else {
String user_ip = request. servervariables ["remote_addr"]. tostring ();
}
5. <% = request. userhostaddress %>

6. Obtain the Client IP Address:

Private string getclientip ()
{
String result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
If (null = Result | result = string. Empty)
{
Result = httpcontext. Current. Request. servervariables ["remote_addr"];
}

If (null = Result | result = string. Empty)
{
Result = httpcontext. Current. Request. userhostaddress;
}
Return result;
}

Obtain the MAC address:

[Dllimport ("iphlpapi. dll")]
Private Static extern int sendarp (int32 DEST, int32 host, ref int64 Mac, ref int32 length );
[Dllimport ("ws2_32.dll")]
Private Static extern int32 inet_addr (string IP );

Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page
Try
{
String userip = request. userhostaddress;
String strclientip = request. userhostaddress. tostring (). Trim ();
Int32 ldest = inet_addr (strclientip); // ip address of the destination
Int32 lhost = inet_addr (""); // ip address of the local server
Int64 macinfo = new int64 ();
Int32 Len = 6;
Int res = sendarp (ldest, 0, ref macinfo, ref Len );
String mac_src = macinfo. tostring ("X ");
If (mac_src = "0 ")
{
If (userip = "127.0.0.1 ")
Response. Write ("accessing localhost! ");
Else
Response. Write ("Welcome to friends from" + userip +! "+" <Br> ");
Return;
}

While (mac_src.length <12)
{
Mac_src = mac_src.insert (0, "0 ");
}

String mac_dest = "";

For (INT I = 0; I <11; I ++)
{
If (0 = (I % 2 ))
{
If (I = 10)
{
Mac_dest = mac_dest.insert (0, mac_src.substring (I, 2 ));
}
Else
{
Mac_dest = "-" + mac_dest.insert (0, mac_src.substring (I, 2 ));
}
}
}

Response. Write ("Welcome to friends from" + userip + "<br>" + "and MAC address" + mac_dest +! "

+ "
");
}< br> catch (exception ERR)
{< br> response. write (err. message);
}< BR >}< br>

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.