【发布时间】:2018-09-03 18:58:48
【问题描述】:
如何在ASP.NET CORE中获取访问者的IPv4、IPv6、网络IP和Mac地址?
我已经使用了下面的代码,但只获得了:::1 值。
var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
【问题讨论】:
标签: asp.net asp.net-mvc ip asp.net-core-mvc
如何在ASP.NET CORE中获取访问者的IPv4、IPv6、网络IP和Mac地址?
我已经使用了下面的代码,但只获得了:::1 值。
var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
【问题讨论】:
标签: asp.net asp.net-mvc ip asp.net-core-mvc
:::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;
};
【讨论】: