[英]How to access session in aspx that was modified in ashx?
我正在使用uploadify上传文件,它们自动发布到处理程序。 然后,我在已设置为网站普通类中的静态属性的处理程序中修改会话。 然后,我尝试在aspx页面中访问相同的会话,并且该值为null。 我感觉这是由于cookie引起的,但是需要一种方法来解决此问题而不暴露url中的sessionid。
ASHX:
public class Upload : IHttpHandler, IReadOnlySessionState, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
...
CMSSession.Current.UploadedFiles.Add(fileName);
}
}
会议课程:
public class CMSSession
{
public static CMSSession Current
{
get
{
CMSSession session = (CMSSession)HttpContext.Current.Session["__CMSSession__"];
if (session == null)
{
session = new CMSSession();
HttpContext.Current.Session["__CMSSession__"] = session;
}
return session;
}
}
public List<string> UploadedFiles { get; set; }
}
ASPX:
if (CMSSession.Current.UploadedFiles != null)
{
...
}
else
{
IT'S ALWAYS NULL
}
Web.Config中:
<sessionState mode="InProc" cookieless="false" /> - causes session to always null in aspx when modified in ashx
<sessionState mode="InProc" cookieless="true" /> - session value is not null, but sessionid is exposed in the url
如何在不将cookieless更改为true的情况下访问和修改ASHX文件中的当前会话,然后从ASPX页访问该会话?
我尝试使用HttpContext并使用传递到ASHX的上下文...没有任何效果。
与此问题相同,但必须有一种更安全的方法: 在ashx中设置会话并在aspx上获取该会话
有任何想法吗?
我找到了答案:从FLASH调用处理程序时(如swfupload或uploadify),它不会将当前sessionid传递给处理程序。 然后,处理程序创建一个NEW会话。 要解决此问题,请执行以下操作:
您的用户界面: JavaScript:
$(Selector).uploadify({
swf: 'uploadify.swf',
uploader: 'Upload.ashx?ASPSESSID=<%=Session.SessionID%>'
});
添加到: Global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
try
{
string session_param_name = "ASPSESSID";
string session_cookie_name = "ASP.NET_SESSIONID";
string session_value = Request.Form[session_param_name] ?? Request.QueryString[session_param_name];
if (session_value != null) { UpdateCookie(session_cookie_name, session_value); }
}
catch (Exception) { }
}
void UpdateCookie(string cookie_name, string cookie_value)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
if (cookie == null)
{
HttpCookie cookie1 = new HttpCookie(cookie_name, cookie_value);
Response.Cookies.Add(cookie1);
}
else
{
cookie.Value = cookie_value;
HttpContext.Current.Request.Cookies.Set(cookie);
}
}
从以下网址进行上传和简化: http ://snipplr.com/view/15180/
如果使用表单身份验证,则可能需要使用authid:
&AUTHID=<%= Request.Cookies[FormsAuthentication.FormsCookieName] == null ? "" : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>
将其附加到jQuery中的uploader参数。
然后将以下内容添加到全局:
try
{
string auth_param_name = "AUTHID";
string auth_cookie_name = FormsAuthentication.FormsCookieName;
string auth_value = Request.Form[auth_param_name] ?? Request.QueryString[auth_param_name];
if (auth_value != null) { UpdateCookie(auth_cookie_name, auth_value); }
}
catch (Exception) { }
您现在可以在IE,Chrome,FF等中从处理程序访问同一会话(甚至使用我在问题中上面使用的静态会话对象)。
Context.Session is null
.. 因为与HttpHandler
连接还有另一个Context.Session
(调试和试: Context.Session.SessionId
在哪里是的FileInput不同于Context.Session.SessionId
在Upload.ashx)!
我建议一种解决方法: 传递对第二个会话中所需元素的引用 (在我的示例中,我使用sessionId变量传递原始SessionId
)
....
var sessionId = "<%=Context.Session.SessionID%>";
var theString = "other param,if needed";
$(document).ready(function () {
$('#fileInput').uploadify({
'uploader': '<%=ResolveUrl("~/uploadify/uploadify.swf")%>',
'script': '<%=ResolveUrl("~/Upload.ashx")%>',
'scriptData': { 'sessionId': sessionId, 'foo': theString },
'cancelImg': '<%=ResolveUrl("~/uploadify/cancel.png")%>',
....
并在.ashx文件中使用此项目。
public void ProcessRequest(HttpContext context)
{
try
{
HttpPostedFile file = context.Request.Files["Filedata"];
string sessionId = context.Request["sessionId"].ToString();
....
如果您需要共享复杂的元素,请使用Context.Application而不是Context.Session ,并使用原始SessionID: Context.Application["SharedElement"+SessionID]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.