c# - ASP 的 Request.IsLocal 在 Azure 中始终为 true

标签 c# asp.net azure

我正在尝试检测网站是否在本地运行以显示堆栈跟踪而不是漂亮的服务器错误页面。我一直很高兴在本地和内部环境中的 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/

相关文章:

asp.net - 无法更改仅在 css asp.net 中具有图像 html 的 div 的背景颜色

Azure CLI 命令在 azure 函数应用程序中不起作用(门户)

azure - 有没有办法从 Azure Runbook 进行 API 调用?

c# - 如何释放虚拟内存?

c# - 事件处理程序会阻止垃圾收集的发生吗?

c# - S : new() in c# 是什么意思

c# - 解决此错误需要进行哪些基本检查-找不到类型或 namespace 名称

asp.net - 使用 ASP .Net MVC4 在 Web API 中处理集合内的集合

.net - 在代码中执行结果集分组,而不是在数据库级别

Azure VM 虚拟网络相互通信