2 dailiboy dailiboy 于 2014.09.17 16:36 提问

asp.net 2.0 C#获取客户端MAC地址。
mac

public partial class 测试 : 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)
{
    if (!Page.IsPostBack)
    {
        aaa.Text = GetMac();
    }
}

private string 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!");
        //    }
        //}

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

        return mac_dest;
    }
    catch (Exception err)
    {
        Response.Write(err.Message);
        return "00";
    }
}

}

我在本机,用公司局域网的电脑测试是正确的,但是放到服务器上,就不对,所有MAC地址都是00-00-00-00-00-00
请问这个问题如何解决

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!