none
请问高手关于ASP.NET获取客户端IP及MAC地址 RRS feed

  • 问题

  • 我尝试用以下的方法,但只能获取IP,MAC不行,用获取MAC那代码连编译都不能通过,是我的问题吗?请问怎么改

    http://www.cnblogs.com/scgw/archive/2008/06/04/1213926.html

    另外用如下的代码,也是只能获取到IP,用户的计算机名也是显示成IP,也是我的问题吗?

    1. 在ASP.NET中专用属性: 
    获取服务器电脑名:Page.Server.ManchineName 
    获取用户信息:Page.User 
    获取客户端电脑名:Page.Request.UserHostName 
    获取客户端电脑IP:Page.Request.UserHostAddress 

    • 已移动 Bob Wu-MT 2012年9月18日 5:50 (发件人:ADO.NET 与 LINQ)
    2012年9月17日 2:58

答案

  • 你好,

    获得MAC地址有如下几种办法:

    public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }

    或者

    public string GetMACAddress()
    {
        ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMOS.Get();
        string MACAddress = String.Empty;
        foreach (ManagementObject objMO in objMOC)
        {
            if (MACAddress == String.Empty) // only return MAC Address from first card   
            {
                MACAddress = objMO["MacAddress"].ToString();
            }
            objMO.Dispose();
        }
        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }

    或者通过客户端:

    <script language="javascript" type="text/javascript">
        function showMacAddress() {
            var obj = new ActiveXObject("WbemScripting.SWbemLocator");
            var s = obj.ConnectServer(".");
            var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
            var e = new Enumerator(properties);
            var output;
            output = '<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
            output = output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
            while (!e.atEnd()) {
                e.moveNext();
                var p = e.item();
                if (!p) continue;
                output = output + '<tr bgColor="#FFFFFF">';
                output = output + '<td>' + p.Caption; +'</td>';
                output = output + '<td>' + p.MACAddress + '</td>';
                output = output + '</tr>';
            }
            output = output + '</table>';
            document.getElementById("box").innerHTML = output;
        }
    </script>


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    2012年9月19日 7:41
    版主

全部回复

  • Hi hzpemu,

    这是一个asp.net的问题,我会把它移到asp.net与ajax论坛去。


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    2012年9月18日 5:50
  • 你好,

    获得MAC地址有如下几种办法:

    public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }

    或者

    public string GetMACAddress()
    {
        ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMOS.Get();
        string MACAddress = String.Empty;
        foreach (ManagementObject objMO in objMOC)
        {
            if (MACAddress == String.Empty) // only return MAC Address from first card   
            {
                MACAddress = objMO["MacAddress"].ToString();
            }
            objMO.Dispose();
        }
        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }

    或者通过客户端:

    <script language="javascript" type="text/javascript">
        function showMacAddress() {
            var obj = new ActiveXObject("WbemScripting.SWbemLocator");
            var s = obj.ConnectServer(".");
            var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
            var e = new Enumerator(properties);
            var output;
            output = '<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
            output = output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
            while (!e.atEnd()) {
                e.moveNext();
                var p = e.item();
                if (!p) continue;
                output = output + '<tr bgColor="#FFFFFF">';
                output = output + '<td>' + p.Caption; +'</td>';
                output = output + '<td>' + p.MACAddress + '</td>';
                output = output + '</tr>';
            }
            output = output + '</table>';
            document.getElementById("box").innerHTML = output;
        }
    </script>


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    2012年9月19日 7:41
    版主