asp.net中使用uploadify插件上传文件, session中的值丢失的问题

工作中遇到使用uploadify插件上传文件后,后台代码中的session[XXX]值为null的问题,反复跟踪,发现不是值丢失,而是sessionID发生了变化,而引起SessionID发生变化的原因就是因为使用了uplodify插件

解决方法:

<script type="text/javascript">
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ?
string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
var ASPSESSID = "<%= Session.SessionID %>";

$("#uploadifyLogo").uploadify({
...
formData: { ASPSESSID: ASPSESSID, AUTHID: auth }
});
</script>

 

 

Global.asax 文件
protected void Application_BeginRequest(object sender, EventArgs e)
{
/* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
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 { }

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 { }
}

private void UpdateCookie(string cookie_name, string cookie_value)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
if (null == cookie)
{
cookie = new HttpCookie(cookie_name);
}
cookie.Value = cookie_value;
HttpContext.Current.Request.Cookies.Set(cookie);
}

 

 

原文地址:

https://www.cnblogs.com/xust/articles/3522367.html

http://stackoverflow.com/questions/1729179/uploadify-session-and-authentication-with-asp-net-mvc

posted @ 2020-02-22 04:00  空格MM  阅读(242)  评论(0编辑  收藏  举报