如何使用ASP.NET C#获取登录用户的Mac和IP地址

3

我想获取登录系统(Web应用程序)的用户的IP地址和MAC地址。我正在使用以下两行代码:

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

我正在使用这种方法来获取MAC地址,但问题是我得到了计算机上所有适配器的MAC地址。我该如何知道哪一个是正确的?怎么才能获得连接到我的网站的设备的正确MAC地址?还有,如何获取适配器的IP地址?当我在我的电脑上尝试时,它给出了8个适配器和8个MAC地址。我正在尝试在我的电脑上进行操作,我有一条有线连接连接到IntraNet,还有一条无线连接到互联网。

2
5个标签,却没有一个告诉你正在使用哪种编程语言? - Wooble
OP可能认为C#是默认语言。http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx - Roman R.
我正在使用C#,我现在将编辑标题、内容和标签,感谢提醒 :) - msytNadeem
@RomanR. 兄弟,我知道,但是如果用户登录到我的网站,我怎么知道哪一个连接到了网站,我想获取MAC地址并将其存储在数据库中,我应该选择哪一个? - msytNadeem
WebForms、WinForms、WPF、Silverlight... - balexandre
@balexandre ASP.NET 我在标题中写了,我说的是网站,所以它当然是一个 WebForm。 - msytNadeem
6个回答

5

当目标(您网站的服务器)和源头(客户机)之间发生套接字连接时,您永远无法获取与您网站连接的用户的MAC地址,您只能获取源头IP地址。因为在套接字连接(客户端 <--> 服务器)中从未发送MAC地址。要获取用户的IP地址:

using System.Net;

Private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;

return addr[addr.Length-1].ToString();

}

source: Here


MAC地址不是必需的,这与下面链接中的代码相同 :) - msytNadeem

1
using System.Runtime.InteropServices;

[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 static string GetClientMAC(string strClientIP) {
    string mac_dest = "";
    try {
        Int32 ldest = inet_addr(strClientIP);
        Int32 lhost = inet_addr("");
        Int64 macinfo = new Int64();
        Int32 len = 6;
        int res = SendARP(ldest, 0, ref macinfo, ref len);
        string mac_src = macinfo.ToString("X");

        while (mac_src.Length < 12) {
            mac_src = mac_src.Insert(0, "0");
        }

        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));
                }
            }
        }
    } catch (Exception err) {
        throw new Exception("Lỗi " + err.Message);
    }
    return mac_dest;
}

这段代码会帮助你 :)

通常最好解释一下解决方案,而不仅仅是发布一些匿名代码行。您可以阅读《如何撰写良好的答案》和《解释完全基于代码的答案》。 - Anh Pham
3
我认为程序员需要对他的问题有一个解决方案。而一个优秀的程序员能够理解代码的运行方式。 - Henry Nguyen

1

您可以使用以下代码片段获取MAC地址。要实现此功能,您需要在应用程序中添加System.Management命名空间。如果它不可用,则需要从项目的“添加引用”中添加其引用。

using System.Management;
using System.IO;

public string GetMACAddress()
{
    string mac_src = "";
    string macAddress = "";

    foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
        {
            mac_src += nic.GetPhysicalAddress().ToString();
            break;
        }
    }

    while (mac_src.Length < 12)
    {
        mac_src = mac_src.Insert(0, "0");
    }

    for (int i = 0; i < 11; i++)
    {
        if (0 == (i % 2))
        {
            if (i == 10)
            {
                macAddress = macAddress.Insert(macAddress.Length, mac_src.Substring(i, 2));
            }
            else
            {
                macAddress = macAddress.Insert(macAddress.Length, mac_src.Substring(i, 2)) + "-";
            }
        }
    }
    return macAddress;
} 

0

当IIS部署此应用程序并运行代码时...成功运行

   public string GetIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }
    [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 static string GetClientMAC(string strClientIP)
    {
        string mac_dest = "";
        try
        {
            Int32 ldest = inet_addr(strClientIP);
            Int32 lhost = inet_addr("");
            Int64 macinfo = new Int64();
            Int32 len = 6;
            int res = SendARP(ldest, 0, ref macinfo, ref len);
            string mac_src = macinfo.ToString("X");

            while (mac_src.Length < 12)
            {
                mac_src = mac_src.Insert(0, "0");
            }

            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));
                    }
                }
            }
        }
        catch (Exception err)
        {
            throw new Exception("Lỗi " + err.Message);
        }
        return mac_dest;
    }

 protected void Button1_Click1(object sender, EventArgs e)
    {  
        Label1.Text = GetClientMAC(GetIPAddress());
    }

只有当目标IPv4地址在本地子网上(可以直接到达IPv4地址而无需经过任何路由器)时,IPv4地址的物理地址才可用。如果目标IPv4地址不在本地子网上,则SendARP函数将失败。- 来源 - HellBaby

0

MAC地址无法通过.NET或JS应用程序访问。 MAC地址从未通过套接字连接发送,只有IP地址,因此您只能使用以下代码获取IP地址:

public string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
}

-3

首先,了解为什么要获取客户端(用户)的MAC地址会很好,因为 MAC地址并不是唯一的

您只能使用.NET Framework来获取服务器连接,而不能获取用户本身。要获取客户端MAC地址,只能依靠JavaScript和ActiveX控件,这只能在Internet Explorer上使用,因为Firefox已经在几年前停止支持ActiveX控件。

关于此问题的良好阅读材料可以在this article中找到。


非常感谢,不使用“NOT UNIQUE”这个词确实有所帮助 :) 能够获取IP地址了,谢谢 :) - msytNadeem
1
按照定义,它是独一无二的,为什么会不唯一呢?此外,可能性有多大?281,474,976,710,656中只有1个。"以太网MAC地址是一个48位数字,唯一对应一个特定的以太网接口" [IEEE标准] (ps博客不是有效的参考)。 - LokizFenrir
任何人都可以随时更改MAC地址。这通常是为了绕过游戏中的封禁而进行的。这是Windows适配器的属性,可进行编辑。 - Ferd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接