我可以欺骗HttpRequest.Current.Request.IsLocal吗?

Sql*_*yan 10 asp.net httpcontext

我正在运行一个Web应用程序,它显示一些调试行为,如果它在本地运行 - 引用资源字符串等 - 我想在我的笔记本电脑上演示应用程序,我将无法访问互联网,所以它必须是本地的.

该应用程序使用HttpContext.Current.Request.IsLocal来确定它是否在本地运行 - 有什么方法可以欺骗它吗?即使我确实在本地运行,我还是想把它变成"假".

我确实可以访问源代码(并且意识到我可以演示一个"IsLocal"检查被注释掉的构建),但是不想为这个演示做一个特殊的构建.如果需要,我会这样做,但我宁愿使用现有的代码库.

RaM*_*RaM 19

Request.IsLocal属性实现以下代码:

public bool IsLocal { 
            get {
                String remoteAddress = UserHostAddress; 

                // if unknown, assume not local
                if (String.IsNullOrEmpty(remoteAddress))
                    return false; 

                // check if localhost 
                if (remoteAddress == "127.0.0.1" || remoteAddress == "::1") 
                    return true;

                // compare with local address
                if (remoteAddress == LocalAddress)
                    return true;

                return false;
            } 
Run Code Online (Sandbox Code Playgroud)

来源:已编译的源代码(Microsoft:referencesource.microsoft.com)

希望这可以帮助 !


Rex*_*x M 3

这需要在对本地 IIS 实例的请求中欺骗非本地 IP 地址。我认为您花在制作演示版本上的时间可能比尝试使其工作的时间要少。