Uploadify ashx file Context.Session gets null

2020-02-08 07:11发布

I have a file upload in my site which is done using uploadify it uses a ashx page to upload file to database.It works fine in IE but in Mozilla the context.Session is getting null.I have also used IReadOnlySessionState to read session.

how can i get session in Mozilla like IE.

here is the ashx code i have done

public class Upload : IHttpHandler, IReadOnlySessionState 
{    
    HttpContext context;
    public void ProcessRequest(HttpContext context)
    {
        string UserID = context.Request["UserID"];

        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        XmlDocument xDoc = new XmlDocument();
        HttpPostedFile postedFile = context.Request.Files["Filedata"];
        try
        {
            if (context.Session["User"] == null || context.Session["User"].ToString() == "")
            {
                context.Response.Write("SessionExpired");
                context.Response.StatusCode = 200;
            }
            else
            {
                  // does the uploading to database
            }
        }
   }
}

In IE Context.Session["User"] always have the value but in Mozilla it is always null

5条回答
乱世女痞
2楼-- · 2020-02-08 07:25

I have created a function to check session have expired and then pass that as a parameter in script-data of uploadify and in ashx file i check that parameter to see whether session exists or not.if it returns session have expired then upload will not take place.It worked for me. Did not find any issues using that. hope that solve my issue

查看更多
Luminary・发光体
3楼-- · 2020-02-08 07:27

I had a similar problem with an .ashx file. The solution was that the handler has to implement IReadOnlySessionState (for read-only access) or IRequiresSessionState (for read-write access). eg:

public class SwfUploadSupportModule : IHttpHandler, IRequiresSessionState { ... }

These Interfaces do not need any additional code but act as markers for the framework.

Hope that this helps.

Jonathan

查看更多
时光不老,我们不散
4楼-- · 2020-02-08 07:34

Context.Session is null.. because connection to HttpHandler has another Context.Session
(debug and try: Context.Session.SessionId in where is the fileInput is different from Context.Session.SessionId in Upload.ashx)!

I suggest a workaround: pass a reference to the elements you need in the second session ( in my sample i pass the original SessionId using sessionId variable)

....
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")%>',
 ....

and use this items in .ashx file.

public void ProcessRequest(HttpContext context)
{
    try
    {
       HttpPostedFile file = context.Request.Files["Filedata"];
       string sessionId = context.Request["sessionId"].ToString();
      ....

If you need to share complex elements use Context.Application instead of Context.Session, using original SessionID: Context.Application["SharedElement"+SessionID]

查看更多
够拽才男人
5楼-- · 2020-02-08 07:35

You need to add sessionId to uploadify post params and restore ASP.NET_SessionId cookie on the server side on global.asax at OnBeginRequest. It is actually bug with flash and cookies.

I have created module for session and auth cookie restore, to get work flash and asp.net session, so i think it will be useful for your:

public class SwfUploadSupportModule : IHttpModule
{
    public void Dispose()
    {
        // clean-up code here.
    }

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(OnBeginRequest);
    }

    private void OnBeginRequest(object sender, EventArgs e)
    {
        var httpApplication = (HttpApplication)sender;

        /* 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 (httpApplication.Request.Form[session_param_name] != null)
            {
                UpdateCookie(httpApplication, session_cookie_name, httpApplication.Request.Form[session_param_name]);
            }
            else if (httpApplication.Request.QueryString[session_param_name] != null)
            {
                UpdateCookie(httpApplication, session_cookie_name, httpApplication.Request.QueryString[session_param_name]);
            }
        }
        catch
        {
        }

        try
        {
            string auth_param_name = "AUTHID";
            string auth_cookie_name = FormsAuthentication.FormsCookieName;

            if (httpApplication.Request.Form[auth_param_name] != null)
            {
                UpdateCookie(httpApplication, auth_cookie_name, httpApplication.Request.Form[auth_param_name]);
            }
            else if (httpApplication.Request.QueryString[auth_param_name] != null)
            {
                UpdateCookie(httpApplication, auth_cookie_name, httpApplication.Request.QueryString[auth_param_name]);
            }
        }
        catch
        {
        }            
    }

    private void UpdateCookie(HttpApplication application, string cookie_name, string cookie_value)
    {
        var httpApplication = (HttpApplication)application;

        HttpCookie cookie = httpApplication.Request.Cookies.Get(cookie_name);
        if (null == cookie)
        {
            cookie = new HttpCookie(cookie_name);
        }
        cookie.Value = cookie_value;
        httpApplication.Request.Cookies.Set(cookie);
    }
}

Also than you need register above module at web.config:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="SwfUploadSupportModule" type="namespace.SwfUploadSupportModule, application name" />
  </modules>
</system.webServer>
查看更多
闹够了就滚
6楼-- · 2020-02-08 07:39

It's likely to be something failing to be set by the server or sent back on the client.

Step back to a lower level - use a network diagnostic tool such as Fiddler or Wireshark to examine the traffic being sent to/from your server and compare the differences between IE and Firefox.

Look at the headers to ensure that cookies and form values are being sent back to the server as expected.

查看更多
登录 后发表回答