我正在尝试检测网站是否在本地运行以显示堆栈跟踪而不是漂亮的服务器错误页面。我一直很高兴在本地和内部环境中的 Global.asax.cs
文件中使用 Request.IsLocal
,但是当它部署到 Azure 应用程序时,它的行为就像该请求确实是本地的。
根据文档,这实际上检查原始 IP 是否为 127.0.0.1
但我不明白这是怎么回事。这只是一些奇怪的 Azure 底层问题吗?
最佳答案
我没有看到同样的问题。但是,我认为,您可以尝试使用以下代码执行 IsLocal。
public static class HttpRequestExtensions
{
public static bool IsLocal(this HttpRequest req)
{
var connection = req.HttpContext.Connection;
if (connection.RemoteIpAddress != null)
{
if (connection.LocalIpAddress != null)
{
return connection.RemoteIpAddress.Equals(connection.LocalIpAddress);
}
return IPAddress.IsLoopback(connection.RemoteIpAddress);
}
if (connection.RemoteIpAddress == null && connection.LocalIpAddress == null)
{
return true;
}
return false;
}
}
关于c# - ASP 的 Request.IsLocal 在 Azure 中始终为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55954724/