Asp. NET get client IP and MAC address __.net

Source: Internet
Author: User
Tags httpcontext servervariables

Get Client IP:

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

Get MAC Address:

[DllImport ("Iphlpapi.dll")]
private static extern int Sendarp (Int32 dest,int32 host,ref Int64 mac,ref length);
[DllImport ("Ws2_32.dll")]
private static extern Int32 inet_addr (string IP);

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Try
{
String userip=request.userhostaddress;
String strclientip = Request.UserHostAddress.ToString (). Trim ();
Int32 ldest = inet_addr (Strclientip); IP of destination
Int32 lhost = inet_addr (""); IP for 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 ("Visiting localhost!");
Else
Response.Write ("Welcome" from IP for "+ Userip +" friends.) "+" <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" from IP for "+userip+" <br> "+", MAC address for "+mac_dest+" friend. "

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

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.