如何获取用户的ip和mac地址登录我的应用程序 [英] How to get the ip and mac address of users login into my application

查看:124
本文介绍了如何获取用户的ip和mac地址登录我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取访问我的应用程序的系统的ip和mac地址,但是只有返回我的服务器ip而不是它返回用户的系统ip。



请帮助另一种获取用户系统ip和mac地址的方法



谢谢。



我尝试过:



公共字符串GetIP()

{

string hostName =;

hostName = System.Net.Dns.GetHostName();

IPHostEntry ip = System.Net.Dns.GetHostEntry (hostName);

IPAddress [] addr = ip.AddressList;



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

}



公共字符串GetMacAddr()

{

NetworkInterface [ ] nic = NetworkInterface.GetAllNetworkInterfaces();

string sMacAddr = string.Empty;

foreach(Netwo) rkInterface适配器在nic)

{

if(sMacAddr == string.Empty)//仅返回第一张卡的MAC地址

{

IPInterfaceProperties prop = adapter.GetIPProperties();

sMacAddr = adapter.GetPhysicalAddress()。ToString();

}



}

返回sMacAddr;

}

i will like to get the ip and mac address of the systems accessing my application, but what is have is only returningmy server ip instead it returning the system ip of the user.

please kindly help with another method that will get the user system ip and mac address

thank you.

What I have tried:

public string GetIP()
{
string hostName = "";
hostName = System.Net.Dns.GetHostName();
IPHostEntry ip = System.Net.Dns.GetHostEntry(hostName);
IPAddress[] addr = ip.AddressList;

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

public string GetMacAddr()
{
NetworkInterface[] nic = NetworkInterface.GetAllNetworkInterfaces();
string sMacAddr = string.Empty;
foreach (NetworkInterface adapter in nic)
{
if (sMacAddr == string.Empty)//only returns MAC Address from first card
{
IPInterfaceProperties prop = adapter.GetIPProperties();
sMacAddr = adapter.GetPhysicalAddress().ToString();
}

}
return sMacAddr;
}

推荐答案

那是因为C#代码在服务器而不是客户端上执行,所以它总是检索基于服务器的数据,而不是客户端。



你无法获得用户的MAC地址 - 它没有通过路由器。

你可以获得的IP - 这是微不足道的 - 是路由器的IP地址,而不是生成请求的个人计算机。所有通过设备连接到互联网的计算机都会向您显示相同的IP地址。



获取该IP地址非常简单: HttpRequest.UserHostAddress Property(System.Web) [ ^ ]将获取它。
That's because C# code executes on the server, not the client, so it always retrieves Server based data, not Client.

You can't get the user MAC address - it doesn't travel past the router.
And the IP you can get - which is trivial - is the IP address of the router, not the individual computer that generated the request. All the computers connecting to the internet via a device will appear to you with the same IP address.

To get that IP address is trivial: HttpRequest.UserHostAddress Property (System.Web)[^] will fetch it.


这篇关于如何获取用户的ip和mac地址登录我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆