如何在ASP.NET CORE中获取访问者的IPv4,IPv6,网络IP和Mac地址?

问题描述 投票:0回答:1

如何在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个回答
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;
    };
© www.soinside.com 2019 - 2024. All rights reserved.