[asp.net]ashx中session存入,aspx为null的原因(使用flash uploader)

I am using uploadify to upload files, they automatically post to the handler. I then modify the session in the handler that I have setup as a static property in a common class of the website. I then try to access that same session in the aspx page, and the value is null. I have a feeling this is because of cookies, but there needs to be a way to work around this without exposing the sessionid in the url.

 

Solutions

I found the answer: When the handler is being called from FLASH (like swfupload or uploadify) it does not pass the current sessionid to the handler. The handler then creates a NEW session. To fix this, do the following:

$(Selector).uploadify({
    swf: 'uploadify.swf',
    uploader: 'Upload.ashx?ASPSESSID=<%=Session.SessionID%>'   
});

Add to: Global.asax:

 1 void Application_BeginRequest(object sender, EventArgs e)
 2 {
 3     try
 4     {
 5         string session_param_name = "ASPSESSID";
 6         string session_cookie_name = "ASP.NET_SESSIONID";
 7         string session_value = Request.Form[session_param_name] ?? Request.QueryString[session_param_name];
 8         if (session_value != null) { UpdateCookie(session_cookie_name, session_value); }
 9     }
10     catch (Exception) { }
11 }
12 
13 void UpdateCookie(string cookie_name, string cookie_value)
14 {
15     HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
16     if (cookie == null)
17     {
18         HttpCookie cookie1 = new HttpCookie(cookie_name, cookie_value);
19         Response.Cookies.Add(cookie1);
20     }
21     else
22     {
23         cookie.Value = cookie_value;
24         HttpContext.Current.Request.Cookies.Set(cookie);
25     }
26 }

如果有表单验证,请继续查看http://stackoverflow.com/questions/14465314/how-to-access-session-in-aspx-that-was-modified-in-ashx#comment20148862_14465314

转载于:https://www.cnblogs.com/tonghounb/p/3902590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值