【问题标题】:why session id change in firefox为什么 Firefox 中的会话 ID 会发生变化
【发布时间】:2011-07-19 01:15:27
【问题描述】:

我正在使用uploadify上传我的文件,我想保存文件,我想在数据库中保存路径,所以我在会话中和用户提交表单后保存路径。它可以在 Internet Explorer 上运行,但在 Firefox 上由于会话 ID 的更改而无法运行。

如何解决这个问题?

【问题讨论】:

标签: asp.net-mvc-2 firefox session uploadify sessionid


【解决方案1】:

uploadify 插件不发送 cookie,因此服务器无法识别会话。实现此目的的一种可能方法是使用 scriptData 参数将 sessionId 包含为请求参数:

<script type="text/javascript">
    $(function () {
        $('#file').uploadify({
            uploader: '<%= Url.Content("~/Scripts/jquery.uploadify-v2.1.4/uploadify.swf") %>',
            script: '<%= Url.Action("Index") %>',
            folder: '/uploads',
            scriptData: { ASPSESSID: '<%= Session.SessionID %>' },
            auto: true
        });
    });
</script>

<% using (Html.BeginForm()) { %>
    <input id="file" name="file" type="file" />
    <input type="submit" value="Upload" />
<% } %>

这会将 ASPSESSID 参数与文件一起添加到请求中。接下来我们需要在服务器上重建会话。这可以在Global.asax 中的Application_BeginRequest 方法中完成:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string sessionParamName = "ASPSESSID";
    string sessionCookieName = "ASP.NET_SessionId";

    if (HttpContext.Current.Request[sessionParamName] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[sessionCookieName];
        if (null == cookie)
        {
            cookie = new HttpCookie(sessionCookieName);
        }
        cookie.Value = HttpContext.Current.Request[sessionParamName];
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
}

最后,将接收上传的控制器操作可以使用会话:

[HttpPost]
public ActionResult Index(HttpPostedFileBase fileData)
{
    // You could use the session here
    var foo = Session["foo"] as string;
    return View();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多