我在 ASP.Net 网站上使用 Yahoo Uploader(Yahoo UI 库的一部分)来允许用户上传文件。对于那些不熟悉的人来说,上传程序通过使用 Flash 小程序来工作,让我可以更好地控制“文件打开”对话框。我可以指定文件类型的过滤器,允许选择多个文件等。这很棒,但它有以下记录的限制:

由于已知的 Flash 错误,Windows 中 Firefox 中运行的上传程序不会在上传时发送正确的 cookie;它不发送 Firefox cookie,而是发送相应域的 Internet Explorer cookie。作为解决方法,我们建议使用无 cookie 上传方法或将 document.cookie 附加到上传请求。

因此,如果用户使用 Firefox,我就不能依靠 cookie 在上传文件时保留其会话。我需要他们的会议,因为我需要知道他们是谁!作为解决方法,我使用 Application 对象:

Guid UploadID = Guid.NewGuid();
Application.Add(Guid.ToString(), User);

因此,我正在创建一个唯一的 ID 并将其用作存储 Page.User 应用程序范围内的对象。上传文件时,我将该 ID 作为变量包含在 POST 中。然后,在接受文件上传的处理程序中,我这样获取 User 对象:

IPrincipal User = (IPrincipal)Application[Request.Form["uploadid"]];

这确实有效,但它有两个明显的缺点:

  • 如果 IIS、应用程序池,甚至只是应用程序在用户访问上传页面和实际上传文件之间重新启动,则它们的“uploadid”将从应用程序范围中删除,并且上传失败,因为我无法对它们进行身份验证。

  • 如果我扩展到网络农场(甚至可能是网络花园)场景,这将完全崩溃。我可能不会担心,但我确实计划在未来扩展这个应用程序。

有人有更好的方法吗?有没有办法让我在 POST 变量中传递实际的 ASP.Net 会话 ID,然后在另一端使用该 ID 来检索会话?

我知道我可以通过以下方式获取会话 ID Session.SessionID, ,并且我知道如何使用 YUI 将其发布到下一页。我不知道如何使用它 SessionID 从状态服务器获取会话。

是的,我使用状态服务器来存储会话,因此它们会持续应用程序/IIS 重新启动,并将在网络场场景中工作。

有帮助吗?

解决方案

这里 是维护者的帖子 SWF上传 它解释了如何从 Request.Form 中存储的 ID 加载会话。我想同样的事情也适用于雅虎组件。

请注意帖子底部的安全免责声明。


通过包含 Global.asax 文件和以下代码,您可以覆盖丢失的会话 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);
        }
    }
}

安全警告: 不要在不知道自己在做什么的情况下将此代码复制并粘贴到 ASP.Net 应用程序中。它介绍了安全问题和跨站点脚本的可能性。

其他提示

您可以通过以下代码获取当前的SessionID:

string sessionId = HttpContext.Current.Session.SessionID;

然后您可以将其输入隐藏字段,然后通过 YUI 访问该值。

这只是一个获取,所以你希望不会有任何扩展问题。不过,我不知道安全问题。

依靠 这篇博文, ,这里有一个函数,可以根据会话 ID 为您提供任何用户的会话,尽管它并不漂亮:

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;
}

ASP.Net 会话 ID 存储在 Session.SessionID 因此您可以将其设置在隐藏字段中,然后将其发布到下一页。

但是我认为,如果应用程序重新启动,如果不重新启动,sessionID将会过期 将您的会话存储在 sql server 中.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top