Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
i need to get client's local ip adress (LAN) when i try to do it always gives me server's wan ip

here is my code

C#
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"];
}


i tried LOCAL_ADDR but still same it gives server wan ip

does anyone have a soliton for this.
Posted

You can only get the IP of the machine that made the request, that may or may not be the actual client. If the client accesses your site via a proxy then you'll get the proxy address. There is nothing you can do about this. Google "asp.net get client ip" and you'll find this is a very frequently asked question.
 
Share this answer
 
You pretty much can't - and it wouldn't be a lot of use to you anyway, as nearly all LANs use 192.x.y.z addressing. You can't use it to access the client, because it will be assumed to be a local address within your server LAN.

And client IP's don't go any further than the router anyway for pretty much just that reason!

In theory, you could get it under very, very specialised circumstances, but they don;t occur for the vast majority of users: The client must be running IE, he must accept ActiveX controls, he must permit ActiveX controls to run, and you must write an ActiveX control. IE isn't the worlds' most popular browser, and all ActiveX controls are disabled by default as they are a HUGE security risk!
 
Share this answer
 
Comments
Member 10210857 26-Oct-15 12:39pm    
i found a java code gets local ip adress but there is problem it writes lan ip to h1 element but when i try to get value of that h1 element it gives me null same situtation for innertext and innerhtml
<script>
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
if (1 || window.mozRTCPeerConnection) {
rtc.createDataChannel('', { reliable: false });
};
rtc.onicecandidate = function (evt) {
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]; });
document.getElementById('list24').textContent = displayAddrs.join(" or perhaps ") || "n/a";
}
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) {
if (~line.indexOf("a=candidate")) {
var parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf("c=")) {
var parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})(); else {
}
</script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900