<p>I'm using the Yahoo Uploader, part of the Yahoo UI Library, on my ASP.Net website to allow users to upload files. For those unfamiliar, the uploader works by using a Flash applet to give me more control over the FileOpen dialog.  I can specify a filter for file types, allow multiple files to be selected, etc. It's great, but it has the following documented limitation:</p>

<blockquote>
  <p>Because of a known Flash bug, the Uploader running in Firefox in Windows does not send the correct cookies with the upload; instead of sending Firefox cookies, it sends Internet Explorer’s cookies for the respective domain. As a workaround, we suggest either using a cookieless upload method or appending document.cookie to the upload request.</p>
</blockquote>

<p>So, if a user is using Firefox, I can't rely on cookies to persist their session when they upload a file. I need their session because I need to know who they are! As a workaround, I'm using the Application object thusly:</p>

<pre><code>Guid UploadID = Guid.NewGuid();
Application.Add(Guid.ToString(), User);
</code></pre>

<p>So, I'm creating a unique ID and using it as a key to store the <code>Page.User</code> object in the Application scope. I include that ID as a variable in the POST when the file is uploaded. Then, in the handler that accepts the file upload, I grab the User object thusly:</p>

<pre><code>IPrincipal User = (IPrincipal)Application[Request.Form["uploadid"]];
</code></pre>

<p>This actually works, but it has two glaring drawbacks: </p>

<ul>
<li><p>If IIS, the app pool, or even just the application is restarted between the time the user visits the upload page, and actually uploads a file, their "uploadid" is deleted from application scope and the upload fails because I can't authenticate them.</p></li>
<li><p>If I ever scale to a web farm (possibly even a web garden) scenario, this will completely break. I might not be worried, except I do plan on scaling this app in the future.</p></li>
</ul>

<p>Does anyone have a better way? Is there a way for me to pass the actual ASP.Net session ID in a POST variable, then use that ID at the other end to retrieve the session?</p>

<p>I know I can get the session ID through <code>Session.SessionID</code>, and I know how to use YUI to post it to the next page. What I don't know is how to use that <code>SessionID</code> to grab the session from the state server.</p>

<p>Yes, I'm using a state server to store the sessions, so they persist application/IIS restarts, and will work in a web farm scenario.</p>

[{'answer_id': 43353, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 0, 'selected': False, 'text': '<p>The ASP.Net Session ID is stored in <code>Session.SessionID</code> so you could set that in a hidden field and then post it to the next page.</p>\n\n<p>I think, however, that if the application restarts, the sessionID will expire if you do not <a href="http://support.microsoft.com/kb/317604" rel="nofollow noreferrer">store your sessions in sql server</a>.</p>\n'}, {'answer_id': 43356, 'author': 'Seb Nilsson', 'author_id': 2429, 'author_profile': 'https://Stackoverflow.com/users/2429', 'pm_score': 1, 'selected': False, 'text': "<p>You can get your current SessionID from the following code:</p>\n\n<pre><code>string sessionId = HttpContext.Current.Session.SessionID;\n</code></pre>\n\n<p>Then you can feed that into a hidden field maybe and then access that value through YUI.</p>\n\n<p>It's just a get, so you hopefully won't have any scaling problems. Security-problems though, that I don't know.</p>\n"}, {'answer_id': 237682, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://swfupload.org/forum/generaldiscussion/98" rel="noreferrer">Here</a> is a post from the maintainer of <a href="http://swfupload.org" rel="noreferrer">SWFUpload</a> which explains how to load the session from an ID stored in Request.Form. I imagine the same thing would work for the Yahoo component.</p>\n\n<p>Note the security disclaimers at the bottom of the post.</p>\n\n<hr>\n\n<blockquote>\n  <p>By including a Global.asax file and the following code you can override the missing Session ID cookie:</p>\n</blockquote>\n\n<pre><code>using System;\nusing System.Web;\n\npublic class Global_asax : System.Web.HttpApplication\n{\n    private void Application_BeginRequest(object sender, EventArgs e)\n    {\n        /* \n        Fix for the Flash Player Cookie bug in Non-IE browsers.\n        Since Flash Player always sends the IE cookies even in FireFox\n        we have to bypass the cookies by sending the values as part of the POST or GET\n        and overwrite the cookies with the passed in values.\n\n        The theory is that at this point (BeginRequest) the cookies have not been ready by\n        the Session and Authentication logic and if we update the cookies here we\'ll get our\n        Session and Authentication restored correctly\n        */\n\n        HttpRequest request = HttpContext.Current.Request;\n\n        try\n        {\n            string sessionParamName = "ASPSESSID";\n            string sessionCookieName = "ASP.NET_SESSIONID";\n\n            string sessionValue = request.Form[sessionParamName] ?? request.QueryString[sessionParamName];\n            if (sessionValue != null)\n            {\n                UpdateCookie(sessionCookieName, sessionValue);\n            }\n        }\n        catch (Exception ex)\n        {\n            // TODO: Add logging here.\n        }\n\n        try\n        {\n            string authParamName = "AUTHID";\n            string authCookieName = FormsAuthentication.FormsCookieName;\n\n            string authValue = request.Form[authParamName] ?? request.QueryString[authParamName];\n            if (authValue != null)\n            {\n                UpdateCookie(authCookieName, authValue);\n            }\n        }\n        catch (Exception ex)\n        {\n            // TODO: Add logging here.\n        }\n    }\n\n    private void UpdateCookie(string cookieName, string cookieValue)\n    {\n        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName);\n        if (cookie == null)\n        {\n            HttpCookie newCookie = new HttpCookie(cookieName, cookieValue);\n            Response.Cookies.Add(newCookie);\n        }\n        else\n        {\n            cookie.Value = cookieValue;\n            HttpContext.Current.Request.Cookies.Set(cookie);\n        }\n    }\n}\n</code></pre>\n\n<blockquote>\n  <p><strong>Security Warning:</strong> Don\'t just copy and paste this code in to your ASP.Net application without knowing what you are doing. It introduces security issues and possibilities of Cross-site Scripting.</p>\n</blockquote>\n'}, {'answer_id': 25755712, 'author': 'Pluto', 'author_id': 1507941, 'author_profile': 'https://Stackoverflow.com/users/1507941', 'pm_score': 2, 'selected': False, 'text': '<p>Relying on <a href="http://konste.wordpress.com/2013/01/22/asp-net-accessing-any-session-data-by-session-id/" rel="nofollow">this blog post</a>, here\'s a function that should get you the session for any user based on the session ID, though it\'s not pretty:</p>\n\n<pre><code>public SessionStateStoreData GetSessionById(string sessionId)\n{\n    HttpApplication httpApplication = HttpContext.ApplicationInstance;\n\n    // Black magic #1: getting to SessionStateModule\n    HttpModuleCollection httpModuleCollection = httpApplication.Modules;\n    SessionStateModule sessionHttpModule = httpModuleCollection["Session"] as SessionStateModule;\n    if (sessionHttpModule == null)\n    {\n        // Couldn\'t find Session module\n        return null;\n    }\n\n    // Black magic #2: getting to SessionStateStoreProviderBase through reflection\n    FieldInfo fieldInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.NonPublic | BindingFlags.Instance);\n    SessionStateStoreProviderBase sessionStateStoreProviderBase = fieldInfo.GetValue(sessionHttpModule) as SessionStateStoreProviderBase;\n    if (sessionStateStoreProviderBase == null)\n    {\n        // Couldn\'t find sessionStateStoreProviderBase\n        return null;\n    }\n\n    // Black magic #3: generating dummy HttpContext out of the thin air. sessionStateStoreProviderBase.GetItem in #4 needs it.\n    SimpleWorkerRequest request = new SimpleWorkerRequest("dummy.html", null, new StringWriter());\n    HttpContext context = new HttpContext(request);\n\n    // Black magic #4: using sessionStateStoreProviderBase.GetItem to fetch the data from session with given Id.\n    bool locked;\n    TimeSpan lockAge;\n    object lockId;\n    SessionStateActions actions;\n    SessionStateStoreData sessionStateStoreData = sessionStateStoreProviderBase.GetItem(\n        context, sessionId, out locked, out lockAge, out lockId, out actions);\n    return sessionStateStoreData;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43324', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2527/']