【问题标题】:Rackspace Cloud rewrite jpg causes session resetRackspace Cloud rewrite jpg 导致会话重置
【发布时间】:2011-02-11 14:15:55
【问题描述】:

这可能是this question 的.NET 版本。

我有一个包含以下内容的图像脚本:

...
Response.WriteFile(filename);
Response.End();

我正在使用web.config 中的以下重写规则重写 .jpg 文件:

<rule name="Image Redirect" stopProcessing="true">
  <match url="^product-images/(.*).jpg" />
  <conditions>
    <add input="{REQUEST_URI}" pattern="\.(jp?g|JP?G)$" />
  </conditions>
  <action type="Rewrite" url="/product-images/ProductImage.aspx?path=product-images/{tolower:{R:1}}.jpg" />
</rule>

它基本上只是将图像路径重写为查询参数。

问题在于(当然是间歇性地)Mosso 返回了一个新的 ASP Session cookie,它破坏了整个世界。

  • 直接访问静态 .jpg 文件不会导致此问题。
  • 直接访问图像脚本也不会导致它。
  • 仅将 .jpg 文件重写为 .aspx 脚本会导致会话丢失。

这不是一个重定向循环——图像出现了,但是缓存服务器提交了一个新的会话 cookie,它(因为它来自我的主机名)导致会话重置。


我尝试过的事情

(来自 Rackspace 文档How can I bypass the cache?

我在图像脚本本身中添加了Private 可缓存性:

Response.Cache.SetCacheability(HttpCacheability.Private);

我尝试将这些禁用缓存的节点添加到 web.config:

<staticContent>
  <clientCache cacheControlMode="DisableCache" />
</staticContent>

<httpProtocol>
  <customHeaders>
    <add name="Cache-Control private" value="Cache-Control private"
  </customHeaders>
</httpProtocol>

我需要的解决方案

无法禁用浏览器缓存。这意味着涉及Cache.SetNoStore()HttpCacheability.NoCache 的潜在解决方案将不起作用。

交替...

请告诉我为什么无法解决这个问题。

【问题讨论】:

  • 我有一个解决方法,它只使用“images.domain.com”来显示图像 - 如果会话在该图像上丢失,那么就是这样。域它不会损害主站点。

标签: .net url-rewriting session-state mosso rackspace-cloud


【解决方案1】:

尝试使用 httphandler,而不是使用 URL 重写。如果是重写导致 Rackspace Cloud 出现问题,那么这应该可以解决问题,因为它消除了重写的使用:

在您的 App_Code 文件夹中创建一个 httphandler 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
/// <summary>
/// Summary description for ProductImageHandler
/// </summary>
public class ProductImageHandler : IHttpHandler
{
    public ProductImageHandler()
    {

    }

    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpg";

        // get path to image
        string pathString = Path.GetFileNameWithoutExtension(context.Request.RawUrl);

        // open file
        // ... insert your logic for manipulating pathString
        // so that it points to file you want to output
        // maybe something like pathString = "~/images/" + pathString + ".jpg";

        context.Response.WriteFile(pathString);
    }

    #endregion
}

然后在您的 web.config 文件中设置条目,以便此处理程序接收“/product-images/”文件夹中的 JPEG 文件请求:

<httpHandlers>
    <add verb="*" path="*/product-images/*.jpg" type="ProductImageHandler" validate="false"/>
</httpHandlers>

也适用于集成 IIS 模式:

<handlers>
    <add name="ProductImageHandler" preCondition="integratedMode" verb="*" path="*/product-images/*.jpg" type="ProductImageHandler"/>
</handlers>

最后一件事 - 您可能必须创建一个名为“product-images”的物理文件夹,以便 IIS 在找不到该文件夹​​时不会返回 404。

要注意的另一件事是,这可能是完成您需要做的事情的更好方法,因为 httphandler 不必经历正常的 ASP.NET 页面生命周期,这意味着这需要更少的内存和时间在服务器上执行。

祝你好运!

【讨论】:

  • +1 这是迄今为止最好的答案 - 太好了,它让其他人消失了!但我不能接受它,因为按钮不存在。也许这是赏金的一部分?
  • 是的,也许赏金会在这么多小时后消失。不用担心。
【解决方案2】:

事实证明,这是 Rackspace 无法解决的限制。

为了解决这个问题,我不得不使用不同的子域来托管图像 (images.site.com)。 子域只是主站点的别名,但主机名使会话无法在 www.site.com 上重置。

我添加了一些重写以指向我的脚本处理图像检索到 Web.Config 以保持图像从该子域返回,并在用户直接浏览该子域时重定向回来。

另外,我怀疑 Rafe 下面的回答 也解决了这个问题。 如果你遇到这个问题,那就是你应该追求的解决方案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多