使用 Firefox 时会话变量丢失,但在 IE 中有效

发布于 2024-08-30 15:58:30 字数 604 浏览 4 评论 0原文

我在 HttpHandler 中设置 Session 变量,然后在 ASPX 页面的 Page_load 事件中获取其值。我正在使用它进行设置

    public void ProcessRequest(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["Filedata"];
        context.Session["WorkingImage"] = file.FileName;
    }

(在有人建议我检查 file.FileName 的有效性之前,如果我在其中硬编码测试字符串,也会出现同样的问题。)它在 IE 中工作得很好,但在 Firefox 中,会话变量未找到,在以下代码中出现“对象引用未设置为对象的实例”错误:

   protected void Page_Load(object sender, EventArgs e)
   {
        string loc = Session["WorkingImage"].ToString();
   }

有人遇到过这个问题 - 并希望想出一种传递会话变量的方法吗?

I am setting a Session variable in an HttpHandler, and then getting its value in the Page_load event of an ASPX page. I'm setting it using

    public void ProcessRequest(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["Filedata"];
        context.Session["WorkingImage"] = file.FileName;
    }

(And before someone suggests that I check the validity of file.FileName, this same problem occurs if I hard-code a test string in there.) It's working just fine in IE, but in Firefox the Session Variable is not found, getting the "Object reference not set to an instance of an object" error in the following code:

   protected void Page_Load(object sender, EventArgs e)
   {
        string loc = Session["WorkingImage"].ToString();
   }

Has anyone encountered this problem - and hopefully come up with a means for passing the session variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴梦长久 2024-09-06 15:58:30

这是针对 HTTPHandler 的吗?如果这偶然与 Flash 有关,并且 Flash 正在发出请求,您将会非常有兴趣阅读有关 Flash Cookie 错误。基本上,Flash 仅转发 IE cookie。

最简单的修复方法是在 Global.asax 中的 Application_BeginRequest 处调用 CorrectCookie,并将 SessionId 放入 Flash 请求的查询字符串中。

Public Shared Sub correctCookie()
    Try
        Dim session_cookie_name As String = "ASP.NET_SESSIONID"
        Dim session_value As String = HttpContext.Current.Request.QueryString("sid")
        If session_value IsNot Nothing Then
            UpdateCookie(session_cookie_name, session_value)
        End If
    Catch ex As Exception
    End Try
End Sub

Private Shared Sub UpdateCookie(ByVal cookie_name As String, ByVal cookie_value As String)
    Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies.[Get](cookie_name)
    If cookie Is Nothing Then
        Dim cookie1 As New HttpCookie(cookie_name, cookie_value)
        HttpContext.Current.Response.Cookies.Add(cookie1)
    Else
        cookie.Value = cookie_value
        HttpContext.Current.Request.Cookies.[Set](cookie)
    End If
End Sub

This is for an HTTPHandler? If this by some chance has something to do with Flash, and Flash is making the request, you will be very interested in reading about the Flash Cookie Bug. Basically, Flash only forwards IE cookies.

The easist fix is to call correctCookie at Application_BeginRequest in your Global.asax and put the SessionId in the querystring of the Flash request.

Public Shared Sub correctCookie()
    Try
        Dim session_cookie_name As String = "ASP.NET_SESSIONID"
        Dim session_value As String = HttpContext.Current.Request.QueryString("sid")
        If session_value IsNot Nothing Then
            UpdateCookie(session_cookie_name, session_value)
        End If
    Catch ex As Exception
    End Try
End Sub

Private Shared Sub UpdateCookie(ByVal cookie_name As String, ByVal cookie_value As String)
    Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies.[Get](cookie_name)
    If cookie Is Nothing Then
        Dim cookie1 As New HttpCookie(cookie_name, cookie_value)
        HttpContext.Current.Response.Cookies.Add(cookie1)
    Else
        cookie.Value = cookie_value
        HttpContext.Current.Request.Cookies.[Set](cookie)
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文