簡體   English   中英

ASP 的 Request.IsLocal 在 Azure 中始終為真

[英]ASP's Request.IsLocal always true in 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM