如何在ASP.NET CORE中获取访问者的IPv4,IPv6,网络IP和Mac地址?
我已经使用了以下代码,但只获得:::1
值。
var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
答案 0 :(得分:0)
:::1
适用于localhost地址。你做得对。相反,你可以使用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;
};