【问题标题】:Unable to get cookie in ie无法在ie中获取cookie
【发布时间】:2014-12-18 02:28:40
【问题描述】:

我在 cookie 中存储了一些 UserData。它在 chrome 和 mozilla 中运行良好。但在 IE 中无法获取 cookie。我使用的是 IE 版本 10。请帮帮我?

在登录表单中:

        Dim Mydtt As String = "my custom data "
        Dim authTicket = New FormsAuthenticationTicket(2, usrid, Date.Now, Date.Now.AddDays(7), True, Mydtt, "/")

        Dim cookie As New HttpCookie("usrlg", FormsAuthentication.Encrypt(authTicket))
        Response.Cookies.Set(cookie)
        Response.Redirect("~/phed/reports")

在会话开始时:

        Dim LastLoginCookie As HttpCookie
        LastLoginCookie = HttpContext.Current.Request.Cookies.Get("usrlg") ' Return Nothing in IE '
        Dim LastLgTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(LastLoginCookie.Value)
        Dim Mydtt As String = LastLgTicket.UserData

【问题讨论】:

    标签: asp.net asp.net-mvc internet-explorer cookies


    【解决方案1】:

    您可能希望在解密之前检查空值。

    Dim LastLoginCookie = HttpContext.Current.Request.Cookies("usrlg")
    
    If LastLoginCookie IsNot Nothing Then
       Dim LastLgTicket As FormsAuthenticationTicket = 
          FormsAuthentication.Decrypt(LastLoginCookie.Value)
       Dim Mydtt As String = LastLgTicket.UserData
    End If
    

    类似的答案here。

    【讨论】:

    • 您可以尝试使用版本 1 和默认 cookie 路径 - ... New FormsAuthenticationTicket(1, usrid, DateTime.Now, DateTime.Now.AddDays(7), true, Mydtt, FormsAuthentication.FormsCookiePath); 看看我的答案中的链接。
    • 我试过 Dim cookie As New HttpCookie("usrlg", FormsAuthentication.Encrypt(authTicket)) cookie.Secure = FormsAuthentication.RequireSSL cookie.Path = FormsAuthentication.FormsCookiePath cookie.Domain = FormsAuthentication.CookieDomain Response .Cookies.Set(cookie)
    • 它不适用于 IE,但适用于 Chrome。发生这种情况的任何原因?
    • 其他电脑也会出现这种情况吗?如果没有,您的 IE 设置可能是个问题。 我很好奇常规 cookie 是否可以在您的 IE 浏览器中使用。 使用 this code 对其进行测试。
    【解决方案2】:

    据我所知,cookie.Secure 必须正确设置,即false 用于 HTTP,true 用于 HTTPS。 您可以使用Request 对象自动检测协议:

    cookie.Secure = Request.IsSecureConnection;
    Response.Cookies.Set(cookie);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 2012-06-04
      • 2020-07-13
      • 1970-01-01
      • 2012-11-08
      • 2020-07-19
      • 2022-01-12
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多