Flash Cookie Bug

来源:互联网 发布:cassandra数据库简介 编辑:程序博客网 时间:2024/06/02 02:04
搞flash多文件上传搞到几乎吐血,今天解决了最后一个问题
使用了swfupload
IE下一切正常,Firefox下上传正常,但是保存上传信息到数据库的时候发现uploadhandle保存的Session在其他页面里取不到。
调试发现浏览两个不同页面的SessionID都不同,sh*t
速度超快的google浏览器问题同Firefox...

google到问题原因:http://www.swfupload.org/forum/generaldiscussion/383
I want to clarify what I have observed about the Flash Cookie bug.
The Flash Player Plugin for FireFox, Opera and Safari (and probably other non-IE based browsers) has a bug which sends persistent cookies from IE to the upload URL instead of the cookies from the browser. Session only cookies from IE are not sent.

When Flash initializes in the browser its own empty "cookie space"is created. It loads persistent cookies from IE (which you can see in%USERPROFILE%/cookies). In-memory (session) cookies are not loaded.

The cookies from the browser are not loaded in to Flash's cookie space.

Any session cookies created by the upload script are maintainedin-memory in Flash's cookies space. New persistant cookies are createdon disk (which you can see in %USERPROFILE%/cookies) and willimmediately appear in IE. Cookies created in the Flash cookie spacewill not appear in any of the browser's "view cookie" tools.

All Flash Movies share the same per browser cookie space which ismaintained until the browser is closed (i.e., multiple tabs in FireFoxwill share the same Flash cookie space but FireFox and Safari maintainseparate Flash cookie spaces).

I've carefully tested this issue in FireFox 3 and IE 7 on Windows XPPro with Flash Player 9.0.115. I also did some basic testing in Opera9.24 and the Safari Beta for Windows. I plan to create a new demo whichwill demonstrate my findings.

I have not tested this issue on OS X or in Linux.

同时抄来一份解决办法:

Global.asax:

  1. void Application_BeginRequest(object sender, EventArgs e)
  2.     {
  3.         /* Fix for the Flash Player Cookie bug in Non-IE browsers.
  4.          * Since Flash Player always sends the IE cookies even in FireFox
  5.          * we have to bypass the cookies by sending the values as part of the POST or GET
  6.          * and overwrite the cookies with the passed in values.
  7.          * 
  8.          * The theory is that at this point (BeginRequest) the cookies have not been read by
  9.          * the Session and Authentication logic and if we update the cookies here we'll get our
  10.          * Session and Authentication restored correctly
  11.          */
  12.         try
  13.         {
  14.             string session_param_name = "ASPSESSID";
  15.             string session_cookie_name = "ASP.NET_SESSIONID";
  16.             if (HttpContext.Current.Request.Form[session_param_name] != null)
  17.             {
  18.                 UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
  19.             }
  20.             else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
  21.             {
  22.                 UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
  23.             }
  24.         }
  25.         catch (Exception)
  26.         {
  27.             Response.StatusCode = 500;
  28.             Response.Write("Error Initializing Session");
  29.         }
  30.         try
  31.         {
  32.             string auth_param_name = "AUTHID";
  33.             string auth_cookie_name = FormsAuthentication.FormsCookieName;
  34.             if (HttpContext.Current.Request.Form[auth_param_name] != null)
  35.             {
  36.                 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
  37.             }
  38.             else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
  39.             {
  40.                 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
  41.             }
  42.         }
  43.         catch (Exception)
  44.         {
  45.             Response.StatusCode = 500;
  46.             Response.Write("Error Initializing Forms Authentication");
  47.         }
  48.     }
  49.     void UpdateCookie(string cookie_name, string cookie_value)
  50.     {
  51.         HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
  52.         if (cookie == null)
  53.         {
  54.             cookie = new HttpCookie(cookie_name);
  55.             HttpContext.Current.Request.Cookies.Add(cookie);
  56.         }
  57.         cookie.Value = cookie_value;
  58.         HttpContext.Current.Request.Cookies.Set(cookie);
  59.     }
js:
  1.             swfu = new SWFUpload({
  2.                 // Backend Settings
  3.                 upload_target_url: "uploadhandle.aspx"// Relative to the SWF file
  4.                 post_params : {
  5.                     "ASPSESSID" : "<%=Session.SessionID %>"
  6.                 },
  7. ……
最后,鄙视这个bug
原创粉丝点击