本文介绍了我想找到客户机的ip,mac和机器名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序服务器中托管   与该服务器连接的客户端。在这个 articture我想获取客户端机器信息,如ip,mac,machine name。





我的尝试:



public string GetVisitorIPAddress(bool GetLan = false)

{

string visitorIPAddress = HttpContext.Current.Request.ServerVariables [HTTP_X_FORWARDED_FOR];



if(String.IsNullOrEmpty(visitorIPAddress))

visitorIPAddress = HttpContext.Current.Request.ServerVariables [REMOTE_ADDR];



if(string.IsNullOrEmpty(visitorIPAddress))

visitorIPAddress = HttpContext.Current.Request.UserHostAddress;



if(string .IsNullOrEmpty(visitorIPAddress)|| visitorIPAddress.Trim()==:: 1)

{

GetLan = true;

visitorIPAddr ess = string.Empty;

}



if(GetLan)

{

if(string.IsNullOrEmpty(visitorIPAddress))

{

//这是本地(LAN)连接的ID地址

string stringHostName = Dns.GetHostName();

//获取Ip主机条目

IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);

//获取来自IP主机条目地址列表的IP地址

IPAddress [] arrIpAddress = ipHostEntries.AddressList;



尝试

{

visitorIPAddress = arrIpAddress [arrIpAddress.Length - 2] .ToString();

}

catch

{

试试

{

visitorIPAddress = arrIpAddress [0] .ToString();

}

catch

{

试试

{

arrIpAddress = Dns.GetHostAddresses(stringHostName);

visitorIPAddress = arrIpAddress [0] .ToString();

}

catch

{

visitorIPAddress =127.0。 0.1;

}

}

}

}

}

返回visitorIPAddress;

}

解决方案

假设这是基于Web的,您无法在服务器上获取该信息。

客户端PC的IP地址没有意义,因为它是本地网络地址(可能是192.168.xx),它仅与其所连接的本地LAN段相关。您可以获得的IP地址是路由器或其他连接到互联网的地址,并通过它访问互联网的所有设备共享。

MAC地址不会通过路由器传输,所以你无法做到这一点。

和机器名称?出于安全原因,你也无法做到这一点。



事实上,你可以在非常特殊的情况下获得所有这些,而不是99.99%的实例:计算机必须是PC,浏览器必须是IE,必须允许ActiveX控件下载,并且必须允许ActiveX控件运行。默认情况下,后两个都被禁用。


My application is hosted in server. application is available for which client who are connected with that server. In this articture i want to get the client machine info like ip,mac,machine name.



What I have tried:

public string GetVisitorIPAddress(bool GetLan = false)
{
string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (String.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

if (string.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = HttpContext.Current.Request.UserHostAddress;

if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
{
GetLan = true;
visitorIPAddress = string.Empty;
}

if (GetLan)
{
if (string.IsNullOrEmpty(visitorIPAddress))
{
//This is for Local(LAN) Connected ID Address
string stringHostName = Dns.GetHostName();
//Get Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;

try
{
visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
}
catch
{
try
{
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
try
{
arrIpAddress = Dns.GetHostAddresses(stringHostName);
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
visitorIPAddress = "127.0.0.1";
}
}
}
}
}
return visitorIPAddress;
}

解决方案
Assuming this is web based, you can't get that information at your server.
The IP address of the client PC is meaningless, as it's a local network address (probably 192.168.x.x) and it is only relevant to the local LAN segment on which it's attached. The IP address you can get is that of the router or other point of connection to the internet and is shared by all equipment accessing the internet through it.
The MAC address is not transferred past the router so you can't get that either.
And the Machine Name? You can't get that either, for security reasons.

In fact, you can get all of these under very specialized circumstances, which do not occur in 99.99% of instances: the computer has to be a PC, the browser has to be IE, It has to have ActiveX control downloads permitted, and ActiveX controls have to be allowed to run. Both of the last two are disabled by default.


这篇关于我想找到客户机的ip,mac和机器名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:30