ASP.NET获取客户端IP及MAC地址

Posted Archy_Wang_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET获取客户端IP及MAC地址相关的知识,希望对你有一定的参考价值。

朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了;下边分为了C#后台获取的方法和前台javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_∩)O


C#代码获取:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

namespace MyTest

    public partial class mac : System.Web.UI.Page
    
        [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);


        protected void Page_Load(object sender, EventArgs e)
        
            GetMac(); 
        

        //获取客户端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;
        



        //获取MAC地址
        private void GetMac()
        
            // 在此处放置用户代码以初始化页面
            try
            
                string userip = Request.UserHostAddress;
                string strClientIP = Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip
                Int32 lhost = inet_addr("");   //本地服务器的ip
                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("正在访问Localhost!");
                    else
                        Response.Write("欢迎来自IP为" + userip + "的朋友!" + "
");
                    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("欢迎来自IP为" + userip + "
" + ",MAC地址为" + mac_dest + "的朋友!"

                 + "
");
            
            catch (Exception err)
            
                Response.Write(err.Message);
            


        
    

Javascript获取:

<html><HEAD><TITLE>WMI Scripting HTMLTITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
 document.forms[0].txtMACAddr.value=unescape(MACAddr);
 document.forms[0].txtIPAddr.value=unescape(IPAddr);
 document.forms[0].txtDNSName.value=unescape(sDNSName);
 //document.formbar.submit();
  SCRIPT>

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
   if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
   
    if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
    MACAddr = objObject.MACAddress;
    if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
    IPAddr = objObject.IPAddress(0);
    if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
    sDNSName = objObject.DNSHostName;
    
  SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR>HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT>OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223>OBJECT>
<SCRIPT language=JScript>
   var service = locator.ConnectServer();
   var MACAddr ;
   var IPAddr ;
   var DomainAddr;
   var sDNSName;
   service.Security_.ImpersonationLevel=3;
   service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
   SCRIPT>

<FORM id=formfoo name=formbar action=NICPost.asp method=post><INPUT value=00:05:5D:0E:C7:FA name=txtMACAddr> <INPUT value=192.168.0.2 name=txtIPAddr> <INPUT value=typ name=txtDNSName> FORM>BODY>HTML>

 

以上是关于ASP.NET获取客户端IP及MAC地址的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP.NET CORE 3.1 中获取客户端 IP 地址?

ASP.NET Core 3.1 - 如何获取客户端的 IP 地址?

ASP.NET Core获取客户端IP地址

如何在 ASP.NET MVC 中获取客户端的 IP 地址?

用户运行托管 IIS 链接时如何在 asp.net 中获取客户端 IP 地址

在 ASP.NET Core 2.2 中获取客户端的 IP 地址