Uploadify ashx file Context.Session gets null
Asked Answered
K

5

5

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

Knothole answered 27/12, 2010 at 10:50 Comment(4)
Probably a stupid question but have you enabled cookies in Mozilla?Dhumma
@deepu, could you show relevant parts of your code (client and server)?Dhumma
i have added the code sample that is done in the ashx page,i got the context.session quiet well in IE But when i came to check in Mozilla always its getting null and session expired while uploading files.On searching i found its hard to get session in IHttpHandler for Mozilla.Is there any method to get sessionKnothole
thanks for the answer,but i didnt help out since i got many server related issues after using the code.... i just manage a simple solution just an addition to my previous code......... 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 issueKnothole
F
11

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>
Fernandez answered 27/12, 2010 at 11:46 Comment(4)
Can you pls explain more about this method.Knothole
This is httpModule that on each begin request check for 'ASPSESSID' parameter and if parameter was found than recreate cookie. Thats all.Fernandez
So guys if you compile this answer and #1729679 you'll get realy working solutionBodywork
i have a wierd issue, i have tried all the above options but i still not able to update cookie on server, but on my developer machine it is working fine. Anybody faced this kind of issue ??Marcus
V
1

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]

Viewfinder answered 7/10, 2011 at 14:15 Comment(0)
D
0

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.

Deedeeann answered 27/12, 2010 at 11:22 Comment(0)
K
0

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

Knothole answered 28/12, 2010 at 9:30 Comment(0)
S
0

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

Shearer answered 1/4, 2013 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.