如何在ASP.NET CORE中获取IPv4,IPv6,网络IP和访问者的MAC地址?
问题描述:
如何在ASP.NET CORE中获取IPv4,IPv6,网络IP和访问者的MAC地址?如何在ASP.NET CORE中获取IPv4,IPv6,网络IP和访问者的MAC地址?
我已经使用下面的代码,但只获得:::1
值。
var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
答
:::1
是本地主机地址。你是对的。相反,你可以使用JavaScript代码。下面是代码为:
var myIP;
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new RTCPeerConnection({ iceServers: [] }), noop = function() { };
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description
pc.onicecandidate = function (ice) { //listen for candidate events
if (!ice || !ice.candidate || !ice.candidate.candidate) return;
myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
console.log('my IP: ', myIP);
$('#IpAddress').val(myIP);
pc.onicecandidate = noop;
};
感谢您的帮助,但我需要在asp.net核心捕获访问者的IPv4,IPv6,网络IP和Mac地址。请问你能帮我吗? – sindhol