解决SWFUpload在Chrome、Firefox等浏览器下的问题
2012-01-30 21:20
543 查看
SWFUpload是一个非常不错的异步上传组件,但是在Chrome、Firefox等浏览器下使用的时候会有问题。问题如下:为了防止跳过上传页面直接向“接受SWFUpload上传的一般处理程序”(假如是Upload.ashx)发送请求造成WebShell漏洞,我的系统中对于Upload.ashx进行了权限控制,只有登录用户才能进行上传。在IE下没问题,但是在Chrome下运行报错“用户未登录”。
经过搜索得知:因为SWFUpload是靠Flash进行上传的,Flash在IE下会把当前页面的Cookie发到Upload.ashx,但是Chrome、Firefox下则不会把当前页面的Cookie发到Upload.ashx。因为Session是靠Cookie中保存的SessionId实现的,这样由于当前页面的Cookie不会传递给Flash请求的Upload.ashx,因此请求的文件发送到Upload.ashx就是一个新的Session了,当然这个Session就是没有登录的了。
解决这个问题的思路也很简单,那就是手动把SessionId传递给服务器,再服务器端读出SessionId再加载Session。其实解决问题的办法SWFUpload的Demo中已经给出了,那就是在SWFUpload的构造函数中设置post_params参数:
post_params中设定的键值对将会以Form表单的形式传递到Upload.ashx,也就是SWFUpload提供了为请求增加自定义请求参数的接口。
上面的代码把当前页面的SessionId写到ASPSESSID值中,当用户上传文件后,ASPSESSID就会传递到服务器上了,在Global.asax的Application_BeginRequest中添加如下代码:
其中UpdateCookie方法的定义如下:
原理:当用户请求到达ASP.Net引擎的时候Application_BeginRequest方法首先被调用,在方法中看客户端是否提交上来了ASPSESSID,如果有的话则把ASPSESSID的值写入Cookie(以"ASP.NET_SESSIONID"为Key,因为ASP.Net中SessionId就是保存在"ASP.NET_SESSIONID"为Key的Cookie中的),Application_BeginRequest方法后就可以从Cookie中读取到"ASP.NET_SESSIONID"的值还原出页面的Session了。
如果网站中还用到了Membership的FormsAuthentication验证,则还需要把AUTHID也按照SessionID的方法进行处理,这一点是其他讲到SWFUpload这个Bug处理的文章中没有提到的。
在SWFUpload的构造函数中设置post_params参数:
在在Global.asax的Application_BeginRequest中添加如下代码:
经过搜索得知:因为SWFUpload是靠Flash进行上传的,Flash在IE下会把当前页面的Cookie发到Upload.ashx,但是Chrome、Firefox下则不会把当前页面的Cookie发到Upload.ashx。因为Session是靠Cookie中保存的SessionId实现的,这样由于当前页面的Cookie不会传递给Flash请求的Upload.ashx,因此请求的文件发送到Upload.ashx就是一个新的Session了,当然这个Session就是没有登录的了。
解决这个问题的思路也很简单,那就是手动把SessionId传递给服务器,再服务器端读出SessionId再加载Session。其实解决问题的办法SWFUpload的Demo中已经给出了,那就是在SWFUpload的构造函数中设置post_params参数:
swfu = new SWFUpload({ // Backend Settings upload_url: "/Upload.ashx", post_params: { "ASPSESSID": "<%=Session.SessionID %>"},
post_params中设定的键值对将会以Form表单的形式传递到Upload.ashx,也就是SWFUpload提供了为请求增加自定义请求参数的接口。
上面的代码把当前页面的SessionId写到ASPSESSID值中,当用户上传文件后,ASPSESSID就会传递到服务器上了,在Global.asax的Application_BeginRequest中添加如下代码:
var Request = HttpContext.Current.Request; var Response = HttpContext.Current.Response; /* Fix for the Flash Player Cookie bug in Non-IE browsers. * Since Flash Player always sends the IE cookies even in FireFox * we have to bypass the cookies by sending the values as part of the POST or GET * and overwrite the cookies with the passed in values. * * The theory is that at this point (BeginRequest) the cookies have not been read by * the Session and Authentication logic and if we update the cookies here we'll get our * Session and Authentication restored correctly */ try { string session_param_name = "ASPSESSID"; string session_cookie_name = "ASP.NET_SESSIONID"; if (HttpContext.Current.Request.Form[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); } else if (HttpContext.Current.Request.QueryString[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); } } catch (Exception) { Response.StatusCode = 500; Response.Write("Error Initializing Session"); }
其中UpdateCookie方法的定义如下:
static void UpdateCookie(string cookie_name, string cookie_value) { HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); if (cookie == null) { cookie = new HttpCookie(cookie_name); //SWFUpload 的Demo中给的代码有问题,需要加上cookie.Expires 设置才可以 cookie.Expires = DateTime.Now.AddYears(1); HttpContext.Current.Request.Cookies.Add(cookie); } cookie.Value = cookie_value; HttpContext.Current.Request.Cookies.Set(cookie); }
原理:当用户请求到达ASP.Net引擎的时候Application_BeginRequest方法首先被调用,在方法中看客户端是否提交上来了ASPSESSID,如果有的话则把ASPSESSID的值写入Cookie(以"ASP.NET_SESSIONID"为Key,因为ASP.Net中SessionId就是保存在"ASP.NET_SESSIONID"为Key的Cookie中的),Application_BeginRequest方法后就可以从Cookie中读取到"ASP.NET_SESSIONID"的值还原出页面的Session了。
如果网站中还用到了Membership的FormsAuthentication验证,则还需要把AUTHID也按照SessionID的方法进行处理,这一点是其他讲到SWFUpload这个Bug处理的文章中没有提到的。
在SWFUpload的构造函数中设置post_params参数:
swfu = new SWFUpload({ // Backend Settings upload_url: "/AdminHT/UploadArticleImg.ashx", post_params: { "ASPSESSID": "<%=Session.SessionID %>", "AUTHID" : "<%=Request.Cookies[FormsAuthentication.FormsCookieName].Value%>" },
在在Global.asax的Application_BeginRequest中添加如下代码:
try { string auth_param_name = "AUTHID"; string auth_cookie_name = FormsAuthentication.FormsCookieName; if (HttpContext.Current.Request.Form[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); } else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); } } catch (Exception) { Response.StatusCode = 500; Response.Write("Error Initializing Forms Authentication"); }
相关文章推荐
- (转)解决SWFUpload在Chrome、Firefox等浏览器下的问题
- 解决SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- 解决SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- 解决SWFUpload在Chrome、Firefox等浏览器下的问题
- 解决SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- Chrome(谷歌浏览器)和Firefox浏览器flash的swf文件发黑不透明问题解决方法
- 常见浏览器兼容问题及相关解决方法(chrome/IE/firefox)
- 总算解决了swfupload在非IE(chrome firefox opera)统统不能用的问题
- SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- uploadify在ie,chrome等浏览器上传文件成功,而firefox出现http error 404问题的解决方法
- uploadify 3.1 /3.2 在Firefox,Chrome 浏览器下,进入页面请求两次问题解决办法。
- 解决struts2 下载中文名称乱码问题 适应不同三大主流浏览器 chrome28.0.1500.95、 firefox 23.0.1 、 IE11
- (造福中国前端界)纯前端Js完美解决各种汉字urlencode,urldecode,编码解码问题,不借助任何字库,内码表,轻松一行,兼容IE8+,chrome,firefox,safari等主流浏览器
- 浏览器(safari,Chrome,FireFox)进网站乱跳其他网站问题
- 有关parent.frame.cols在firefox浏览器上不兼容的问题解决
- 解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
- 关于IE,Chrome,Firefox浏览器的字符串拼接问题
- 解决使用FireFox下Flash上传文件时SESSION丢失的问题(swfupload)
- 解决各大浏览器下载文件乱码以及Firefox下载文件名不全问题