我正在使用此代码来获取网站访客的实际IP地址,但它返回的PUBLIC IP地址与我们办公室中的每个人都相同,但我需要访客系统的实际ipv4地址。
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
protected string GetIPAddressLocal()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[addresses.Length-1];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
public static IPAddress GetIPAddress3(string hostName)
{
Ping ping = new Ping();
var replay = ping.Send(hostName);
if (replay.Status == IPStatus.Success)
{
return replay.Address;
}
return null;
}
private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
string ipaddress = "";
return ipaddress.ToString();
}
public static string GetUserIP()
{
var ip = (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null
&& System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")
? System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
: System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (ip.Contains(","))
ip = ip.Split(',').First().Trim();
return ip;
}
string ipaddress = GetIPAddress();
ViewBag.ip = ipaddress;
ViewBag.ip2= System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(1).ToString();
ViewBag.ip3 = Request.ServerVariables["REMOTE_ADDR"];
ViewBag.ip4 = System.Web.HttpContext.Current.Request.UserHostAddress;
ViewBag.ip5 = GetIPAddress3(System.Net.Dns.GetHostName());
ViewBag.ip6 = GetIP();
ViewBag.ip7 = GetUserIP();
我使用了多种功能,它返回服务器s ip address or the public ip address which is same for all the users. It doesn
而不返回用户系统拥有的实际IP地址。
答案 0 :(得分:0)
我做到了。
这是解决方案:
$(document).ready(function () {
// NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
console.log(rtc);
if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed
rtc.createDataChannel('', { reliable: false });
};
rtc.onicecandidate = function (evt) {
// convert the candidate to SDP so we can run it through our general parser
// see https://twitter.com/lancestout/status/525796175425720320 for details
if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
};
rtc.createOffer(function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn("offer failed", e); });
var addrs = Object.create(null);
addrs["0.0.0.0"] = false;
function updateDisplay(newAddr) {
if (newAddr in addrs) return;
else addrs[newAddr] = true;
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
if (displayAddrs[0] === null || displayAddrs[0] === '') {
$('#txtIpAddress').val(displayAddrs.join(" or perhaps "));
} else {
$('#txtIpAddress').val(displayAddrs[0]);
}
//$('#txtIpAddress').val(displayAddrs.join(" or perhaps "));
// console.log(displayAddrs[0]);
// alert(displayAddrs.join(" or perhaps "));
// document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";
}
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
var parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})();
});