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/

相关文章:

c# - 将 Rhinoscriptsyntax 从 Python 移植到 C#

c# - 如何将VB语句转换为C#

azure - 是否可以将 System.File.IO 与 Azure 文件共享一起使用?

azure - 使用 OWIN Pipeline 进行 Multi-Tenancy 身份验证

java - C# 中的 string.ToLowerInvariant() 与 Java 中的土耳其语 String.ToLowerCase(Culture.ROOT)

c# - 使用 WPF 实现日志查看器

asp.net - 加密 web.config 连接字符串 - 它是否使用站点的机器 key ?我是否也加密机器 key ?

c# - 如何获取存储过程的返回值

c# - Access 中的 SELECT @@IDENTITY 总是返回 0

azure - Bat 文件中的 AZ 登录