页面如何知道它是否在本地LAN上提供?

问题描述

我的项目是一个Web应用程序,可以用作带有徽标和快速链接的公司目标网页。

我想为本地LAN中的用户向基于LAN的网页添加额外的链接,但是如果该页面是在本地LAN之外提供的,则隐藏这些链接

我知道如何隐藏和显示事物 如何判断该页面是在局域网中还是在局域网之外?

到目前为止,这是我唯一想到的事情,但是我不确定它是否可以工作。 (strings.left()是我自己的函数之一,该函数从字符串的左侧获取前x个字符)

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;
    }
}

我想我需要知道的是,如果使用当前的代码,如果另一个LAN中的用户访问我的页面,我的代码会检测到其内部ipaddress还是外部ipaddress?

解决方法

这可以解决问题。它从局域网外部检测外部ip地址,从局域网内部检测内部ipaddress。而且,如果从具有匹配IP地址的外部LAN访问该页面,它仍会返回外部IP地址。

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;
}

}

https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.userhostaddress#System_Web_HttpRequest_UserHostAddress