Can I put an ASP.Net session ID in a hidden form field?

后端 未结 4 1032
礼貌的吻别
礼貌的吻别 2020-12-05 20:55

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

相关标签:
4条回答
  • 2020-12-05 21:10

    Here is a post from the maintainer of SWFUpload 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.

    Note the security disclaimers at the bottom of the post.


    By including a Global.asax file and the following code you can override the missing Session ID cookie:

    using System;
    using System.Web;
    
    public class Global_asax : System.Web.HttpApplication
    {
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            /* 
            Fix for the Flash Player Cookie bug in Non-IE browsers.
            Since Flash Player always sends the IE cookies even in FireFox
            we have to bypass the cookies by sending the values as part of the POST or GET
            and overwrite the cookies with the passed in values.
    
            The theory is that at this point (BeginRequest) the cookies have not been ready by
            the Session and Authentication logic and if we update the cookies here we'll get our
            Session and Authentication restored correctly
            */
    
            HttpRequest request = HttpContext.Current.Request;
    
            try
            {
                string sessionParamName = "ASPSESSID";
                string sessionCookieName = "ASP.NET_SESSIONID";
    
                string sessionValue = request.Form[sessionParamName] ?? request.QueryString[sessionParamName];
                if (sessionValue != null)
                {
                    UpdateCookie(sessionCookieName, sessionValue);
                }
            }
            catch (Exception ex)
            {
                // TODO: Add logging here.
            }
    
            try
            {
                string authParamName = "AUTHID";
                string authCookieName = FormsAuthentication.FormsCookieName;
    
                string authValue = request.Form[authParamName] ?? request.QueryString[authParamName];
                if (authValue != null)
                {
                    UpdateCookie(authCookieName, authValue);
                }
            }
            catch (Exception ex)
            {
                // TODO: Add logging here.
            }
        }
    
        private void UpdateCookie(string cookieName, string cookieValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName);
            if (cookie == null)
            {
                HttpCookie newCookie = new HttpCookie(cookieName, cookieValue);
                Response.Cookies.Add(newCookie);
            }
            else
            {
                cookie.Value = cookieValue;
                HttpContext.Current.Request.Cookies.Set(cookie);
            }
        }
    }
    

    Security Warning: 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.

    0 讨论(0)
  • 2020-12-05 21:10

    You can get your current SessionID from the following code:

    string sessionId = HttpContext.Current.Session.SessionID;
    

    Then you can feed that into a hidden field maybe and then access that value through YUI.

    It's just a get, so you hopefully won't have any scaling problems. Security-problems though, that I don't know.

    0 讨论(0)
  • 2020-12-05 21:11

    Relying on this blog post, here's a function that should get you the session for any user based on the session ID, though it's not pretty:

    public SessionStateStoreData GetSessionById(string sessionId)
    {
        HttpApplication httpApplication = HttpContext.ApplicationInstance;
    
        // Black magic #1: getting to SessionStateModule
        HttpModuleCollection httpModuleCollection = httpApplication.Modules;
        SessionStateModule sessionHttpModule = httpModuleCollection["Session"] as SessionStateModule;
        if (sessionHttpModule == null)
        {
            // Couldn't find Session module
            return null;
        }
    
        // Black magic #2: getting to SessionStateStoreProviderBase through reflection
        FieldInfo fieldInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.NonPublic | BindingFlags.Instance);
        SessionStateStoreProviderBase sessionStateStoreProviderBase = fieldInfo.GetValue(sessionHttpModule) as SessionStateStoreProviderBase;
        if (sessionStateStoreProviderBase == null)
        {
            // Couldn't find sessionStateStoreProviderBase
            return null;
        }
    
        // Black magic #3: generating dummy HttpContext out of the thin air. sessionStateStoreProviderBase.GetItem in #4 needs it.
        SimpleWorkerRequest request = new SimpleWorkerRequest("dummy.html", null, new StringWriter());
        HttpContext context = new HttpContext(request);
    
        // Black magic #4: using sessionStateStoreProviderBase.GetItem to fetch the data from session with given Id.
        bool locked;
        TimeSpan lockAge;
        object lockId;
        SessionStateActions actions;
        SessionStateStoreData sessionStateStoreData = sessionStateStoreProviderBase.GetItem(
            context, sessionId, out locked, out lockAge, out lockId, out actions);
        return sessionStateStoreData;
    }
    
    0 讨论(0)
  • 2020-12-05 21:24

    The ASP.Net Session ID is stored in Session.SessionID so you could set that in a hidden field and then post it to the next page.

    I think, however, that if the application restarts, the sessionID will expire if you do not store your sessions in sql server.

    0 讨论(0)
提交回复
热议问题