如何配置 ASP.Net OutputCache 以根据 http 与 https 进行变化?

发布于 2024-07-27 14:25:58 字数 530 浏览 10 评论 0原文

以下是这样的场景,用户在浏览器中从我们的 WebApp 打开非安全页面(我们称之为 PageA),然后单击其中的链接将他们带到 PageB 的安全实例。 进入 PageB 后,用户随后可以单击一个链接,将他们带回到 PageA 的安全实例(他们已经查看过该实例并且位于 OutputCache 中)。 我观察到,即使在访问 PageB(安全的)后通过不同的 URL 访问 PageA,它实际上是拉取先前缓存的副本,而不是制作一个新的副本。 我在调试会话中验证了此行为,并对 ASP.Net 使用相同的 OutputCache 项来获取页面的安全副本感到惊讶。

我的问题是为什么会这样? 我如何告诉 ASP.Net OutPutCache 将来自安全 URL 的访问视为与非安全等效项不同/唯一的项目?

[背景]

我们最近将网站图像切换为对所有图像使用 Scene7/Akamai。 因此,我们添加了代码,以便在安全连接上查看给定页面时使用不同的 Scene7 url。 此 OutputCache 问题不允许执行输出安全 URL 的逻辑,并导致浏览器发出难看的警告。

Here is the scenario, a user opens up non-secure page from our WebApp, let's call it PageA, in their browser and then clicks a link in there that takes them to a secure instance of PageB. Once in PageB the user can subsequently click a link that takes them back to a secure instance of PageA (which they already viewed and is in OutputCache). I observed that even though PageA is being accessed over a different URL after visiting PageB (the secure one) it's actually pulling the prior cached copy rather making a fresh one. I verified this behavior in a debugging session, and was surprised that ASP.Net used the same OutputCache item for a secure copy of the page.

My question is why is it this way? And how do I tell the ASP.Net OutPutCache to treat access from secure URL as a different/unique item than the non-secure equivalent?

[Background]

We recently switched our Web Sites images over to use Scene7/Akamai for all images. As a result of this we added code to use different Scene7 url's when viewing a given page on a secure connection. This OutputCache issue is not allowing for the logic that outputs the secure url's to execute, and is resulting in ugly browser warnings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-08-03 14:25:58

这并不能回答问题的措辞,但它可能会消除您因方案而异的需要。 如果您对 Scene7 url 的“http://”进行硬编码,则可以将它们更改为方案相对 url。

<img src="http://site.scene7.com/images/someimage.jpg" />
=>
<img src="//site.scene7.com/images/someimage.jpg" />

这将导致浏览器自动请求与引用页面具有相同方案的资源。 当然,这是假设您拥有 scene7 域的 SSL 证书。

This doesn't answer the question as worded but it may eliminate your need to vary by scheme. If you are hard coding the "http://" for the Scene7 urls you can change them to scheme-relative urls.

<img src="http://site.scene7.com/images/someimage.jpg" />
=>
<img src="//site.scene7.com/images/someimage.jpg" />

That will cause the browser to automatically ask for the resource with the same scheme as the referring page. That's assuming you have an SSL certificate for your scene7 domain of course.

丶视觉 2024-08-03 14:25:58

我认为您可以执行 VaryByCustom="scheme" 并将其添加到您的 Global.asax.cs 文件中(包括我使用的其他几个以及应用程序版本和用户):

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom.Equals("version", StringComparison.CurrentCultureIgnoreCase))
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] parts = asm.FullName.Split(',');
            string version = parts[1].Trim().ToLower();
            return version;
        }
        else if (custom.Equals("user", StringComparison.CurrentCultureIgnoreCase))
        {
            var user = Membership.Users.CurrentUser;
            return null == user ? string.Empty : user.Id.ToString();
        }
        else if (custom.Equals("scheme", StringComparison.CurrentCultureIgnoreCase))
        {
            var scheme = context.Request.IsSecureConnection ? "https" : "http";
            return scheme;
        }
        else
            return base.GetVaryByCustomString(context, custom);
    }

I think that you can do a VaryByCustom="scheme" and add this to your Global.asax.cs file (inlcuding a couple other others that I use as well app version & user):

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom.Equals("version", StringComparison.CurrentCultureIgnoreCase))
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] parts = asm.FullName.Split(',');
            string version = parts[1].Trim().ToLower();
            return version;
        }
        else if (custom.Equals("user", StringComparison.CurrentCultureIgnoreCase))
        {
            var user = Membership.Users.CurrentUser;
            return null == user ? string.Empty : user.Id.ToString();
        }
        else if (custom.Equals("scheme", StringComparison.CurrentCultureIgnoreCase))
        {
            var scheme = context.Request.IsSecureConnection ? "https" : "http";
            return scheme;
        }
        else
            return base.GetVaryByCustomString(context, custom);
    }
痴情换悲伤 2024-08-03 14:25:58

我从未尝试过,但您也许可以使用 Outputcache VaryByHeader 属性和“主机”标头,该标头指定所请求资源的 Internet 主机和端口号。

我的问题是,为什么您要从 PageB 重定向到安全页面 A。 如果它是非安全页面,您不能修复 PageB 重定向以始终重定向到非安全页面吗?

I've never tried it but you might be able to use the Outputcache VaryByHeader property and the "host" header, which specifies the Internet host and port number of the resource being requested.

The question I'd have is why are you redirecting to PageA over secure after from PageB. If its a non-secure page, couldn't you fix the PageB redirect to always redirect to non-secure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文