获取访问的客户端的ip地址和mac地址

服务端需要实现一个功能是获取访问的客户端的ip地址和mac地址,如何实现,附实例
C# MVC

从 request 里面可以 例如
Request.ServerVariables.Get("Remote_Addr").ToString();
Request.ServerVariables("Url")
返回服务器地址

Request.ServerVariables("Path_Info")
客户端提供的路径信息

Request.ServerVariables("Appl_Physical_Path")
与应用程序元数据库路径相应的物理路径

Request.ServerVariables("Path_Translated")
通过由虚拟至物理的映射后得到的路径

Request.ServerVariables("Script_Name")
执行脚本的名称

Request.ServerVariables("Query_String")
查询字符串內容

Request.ServerVariables("Http_Referer")
请求的字符串內容

Request.ServerVariables("Server_Port")
接受请求的服务器端口号

Request.ServerVariables("Remote_Host")
发出请求的远程主机名称

Request.ServerVariables("Http_Host")
返回服务器地址

Request.ServerVariables("Server_Name")
服务器的主机名、DNS地址或IP地址

Request.ServerVariables("Request_Method")
提出请求的方法比如GET、HEAD、POST等等

Request.ServerVariables("Server_Port_Secure")
如果接受请求的服务器端口为安全端口时,则为1,否则为0

Request.ServerVariables("Server_Protocol")
服务器使用的协议的名称和版本

Request.ServerVariables("Server_Software")
应答请求并运行网关的服务器软件的名称和版本

Request.ServerVariables("All_Http")
客户端发送的所有HTTP标头,前缀HTTP_

Request.ServerVariables("All_Raw")
客户端发送的所有HTTP标头,其结果和客户端发送时一样,没有前缀HTTP_

Request.ServerVariables("Appl_MD_Path")
应用程序的元数据库路径

Request.ServerVariables("Content_Length")
客户端发出內容的长度

Request.ServerVariables("Https")
如果请求穿过安全通道(SSL),则返回ON如果请求来自非安全通道,则返回OFF

Request.ServerVariables("Instance_ID")
IIS实例的ID号

Request.ServerVariables("Instance_Meta_Path")
响应请求的IIS实例的元数据库路径

Request.ServerVariables("Http_Accept_Encoding")
返回內容如:gzip,deflate

Request.ServerVariables("Http_Accept_Language")
返回內容如:en-us

Request.ServerVariables("Http_Connection")
返回內容:Keep-Alive

Request.ServerVariables("Http_Cookie")
返 回內容如:nVisiT%2DYum=125;ASPSESSIONIDCARTQTRA=FDOBFFABJGOECBBKHKGPFIJI; ASPSESSIONIDCAQQTSRB=LKJJPLABABILLPCOGJGAMKAM;ASPSESSIONIDACRRSSRA=DKHHHFBBJOJCCONPPHLKGHPB

Request.ServerVariables("Http_User_Agent")
返回內容:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)

Request.ServerVariables("Https_Keysize")
安全套接字层连接关键字的位数,如128

Request.ServerVariables("Https_Secretkeysize")
服务器验证私人关键字的位数如1024

Request.ServerVariables("Https_Server_Issuer")
服务器证书的发行者字段

Request.ServerVariables("Https_Server_Subject")
服务器证书的主题字段

Request.ServerVariables("Auth_Type")
是用户访问受保护的脚本时,服务器用於检验用户的验证方法

Request.ServerVariables("Auth_User")
代证的用户名

Request.ServerVariables("Cert_Cookie")
唯一的客户证书ID号

Request.ServerVariables("Cert_Flag")
客户证书标誌,如有客户端证书,则bit0为0如果客户端证书验证无效,bit1被设置为1

Request.ServerVariables("Cert_Issuer")
用户证书中的发行者字段

Request.ServerVariables("Cert_Keysize")
安全套接字层连接关键字的位数,如128

Request.ServerVariables("Cert_Secretkeysize")
服务器验证私人关键字的位数如1024

Request.ServerVariables("Cert_Serialnumber")
客户证书的序列号字段

Request.ServerVariables("Cert_Server_Issuer")
服务器证书的发行者字段

Request.ServerVariables("Cert_Server_Subject")
服务器证书的主题字段

Request.ServerVariables("Cert_Subject")
客户端证书的主题字段

Request.ServerVariables("Content_Type")
客户发送的form內容或HTTPPUT的数据类型

jiyugpt

在C# MVC中,你可以通过以下方式获取访问客户端的IP地址和MAC地址:

获取客户端IP地址:

string ipAddress = Request.UserHostAddress;

获取客户端MAC地址:
在Web开发中,通常无法直接获取客户端的MAC地址,因为MAC地址是在局域网内使用的硬件地址,不会在HTTP请求中传输。HTTP请求只包含源IP地址和目标IP地址。如果您需要获取客户端的MAC地址,可以考虑使用其他技术,如ARP协议或使用JavaScript等在客户端执行的方式。

在C#中,可以使用NetworkInterface类来获取本地计算机的MAC地址。以下是获取本地计算机的MAC地址的示例代码:

using System.Net.NetworkInformation;

string macAddress = string.Empty;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
    if (nic.OperationalStatus == OperationalStatus.Up)
    {
        macAddress = nic.GetPhysicalAddress().ToString();
        break;
    }
}

请注意,上述代码将获取本地计算机的MAC地址,而不是客户端的MAC地址。在Web应用程序中,无法直接从客户端获取MAC地址,因为MAC地址通常是在局域网中使用的,而不是在Internet上公开传输的。

客户端的IP地址可以从Request中获取(需要考虑是否使用代理等情况)。MAC地址比较麻烦,Request中并不会携带这些信息。
你可以参考下面的文章
https://blog.csdn.net/allsharps/article/details/7357446
https://blog.csdn.net/wang379275614/article/details/17596135

客户端IP地址可以获取, 获取socket的RemoteEndPoint属性即可。 参考msdn示例。
https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.remoteendpoint?view=net-7.0#examples

C# Mvc示例
https://blog.csdn.net/csdn_4399/article/details/122860284

客户端MAC地址服务端获取不了,建议由客户端自己来获取mac地址,然后发送给服务端。

用Request.ServerVariables( "REMOTE_ADDR ") 来取得客户端的IP地址,如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables( "HTTP_X_FORWARDED_FOR ") 来读取。
具体代码,请移步:https://blog.csdn.net/m0_37886901/article/details/102565305

网关中这么写

img

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7764781
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:通过mac地址找设备的IP地址
  • 除此之外, 这篇博客: 八股文:计算机网络中的 为啥有IP地址还需要MAC地址?为啥有MAC地址还需要IP地址 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 首先是有交换机用来寻找地址的,交换机中是根据MAC地址来寻址的,但是随着电脑越来越多,不够交换机去分配,于是有了路由器,路由器起到对应于不同子网的IP地址电脑之间的转发上,也就是说如果是在相同的子网上的话,是可以直接通过交换机来实现通信的,而对于不同子网上的电脑,则需要使用IP地址来通信。

获取客户端的 IP 地址可以使用 HttpContext.Current.Request.UserHostAddress 方法,而获取 MAC 地址则需要通过一些额外的步骤来解决。由于 MAC 地址是在网络层中使用的,因此它只能从 LAN 中的路由器或交换机上获取。以下是一个实现方法:

csharp

public string GetClientIPAddress()
{
    string ipAddress = HttpContext.Current.Request.UserHostAddress;

    // For IPv4 addresses, we can get the MAC address
    if (ipAddress != null && ipAddress.Contains("."))
    {
        IPAddress clientIP = IPAddress.Parse(ipAddress);
        byte[] macAddr = new byte[6];
        uint macAddrLen = (uint)macAddr.Length;

        // Send ARP request to get MAC address from router
        if (SendARP(BitConverter.ToInt32(clientIP.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen) == 0)
        {
            string[] str = new string[(int)macAddrLen];
            for (int i = 0; i < macAddrLen; i++)
                str[i] = macAddr[i].ToString("x2");

            return string.Join(":", str); // Return formatted MAC address
        }
    }

    return null; // MAC address not found
}

请注意,此方法仅适用于 IPv4 地址,并且需要在 Windows 系统上运行。此外,要使用 System.Net.NetworkInformation 命名空间中的 SendARP 方法,您需要在代码文件中添加以下引用:

csharp
using System.Net.NetworkInformation;

直接让客户端传mac地址啊,
IP 地址可以通过 Request.UserHostAddress 属性来实现

using System.Web;  
  
public string GetClientIPAddress()  
{  
    return HttpContext.Current.Request.UserHostAddress;  
}

从socket信息可以获取到IP,但是mac地址,只有同一个局域网有可能,出了局域网是获取不到的,除非客户端在传输层数据里给。局域网里可以根据IP地址和本地ARP缓冲获取


package com.howin.util;
 
import java.net.*;
 
public class Ipconfig {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        InetAddress ia=null;
        try {
            ia=ia.getLocalHost();
             
            String localname=ia.getHostName();
            String localip=ia.getHostAddress();
            System.out.println("本机名称是:"+ localname);
            System.out.println("本机的ip是 :"+localip);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

mac地址一是公网获取不到
二是部分手机会随机生成


csharp
public string GetClientIpAddress()
{
    string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     if (string.IsNullOrEmpty(ipAddress))
    {
        ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
     return ipAddress;
}
 public string GetClientMacAddress()
{
    string macAddress = "";
     try
    {
        string ipAddress = GetClientIpAddress();
         Process process = new Process();
        process.StartInfo.FileName = "arp";
        process.StartInfo.Arguments = "-a " + ipAddress;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();
         string output = process.StandardOutput.ReadToEnd();
        string[] lines = output.Split('\n');
         if (lines.Length >= 3)
        {
            macAddress = lines[2].Substring(24, 17);
        }
    }
    catch (Exception ex)
    {
        // 处理异常
    }
     return macAddress;
}

其中,GetClientIpAddress()函数用于获取客户端的IP地址,GetClientMacAddress()函数用于获取客户端的MAC地址。需要注意的是,获取MAC地址需要在服务端执行,而且需要使用管理员权限运行应用程序。