My project is a web-application that is to serve as a company Landing Page with a logo and quick links.
I would like to add extra links to LAN-based webpages for users who are in my local LAN, but hide those links if the page is served outside the local LAN.
I know how to hide and show things
How can I tell whether the page is served in the LAN or outside the LAN?
This is the only thing I’ve come up with so far, but I’m not sure it will work. (strings.left() is one of my own function that fetches the first x characters from the left of a string)
protected void Page_Load(object sender, EventArgs e) {
String thisipaddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (thisipaddress == "127.0.0.1" || strings.left(thisipaddress,7)=="192.168"){
hiddenarea.Visible = true;
} else {
hiddenarea.Visible = false;
}
}
I guess what I need to know is, with my current code, if a user in another LAN access my page, will my code detect their internal ipaddress or their external one?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This does the trick. It detects external ip address from outside the lan and internal ipaddress from inside the lan. And if the page is accessed from an external LAN with a matching ipaddress, it still returns the external one.
protected void Page_Load(object sender, EventArgs e) {
String thisipaddress = Request.UserHostAddress; <-----THIS
if (thisipaddress == "127.0.0.1" || strings.left(thisipaddress,7)=="192.168"){
hiddenarea.Visible = true;
} else {
hiddenarea.Visible = false;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0