【问题标题】:How to get IPv4, IPv6, Network IP and Mac address of visitors in ASP.NET CORE?如何在 ASP.NET CORE 中获取访问者的 IPv4、IPv6、网络 IP 和 Mac 地址?
【发布时间】: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


    【解决方案1】:

    :::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 地址。请问您能帮我解决这个问题吗?
    • 问题是通过 .NET Core 得到它,而你只是说“你可以使用 JS”
    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2023-04-11
    • 2013-03-31
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多