0

I have stored some UserData in cookie. It works fine in chrome and mozilla. But in IE its unable to get cookie.I am using IE Version 10. Please help me out ?

In Login form :

        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")

In Session Start :

        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

2 Answers2

0

You might want to check for null value before decrypting.

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

Similar answer here.

Community
  • 1
  • 1
Win
  • 61,100
  • 13
  • 102
  • 181
  • Could you try with version 1 and default cookie path - `... New FormsAuthenticationTicket(1, usrid, DateTime.Now, DateTime.Now.AddDays(7), true, Mydtt, FormsAuthentication.FormsCookiePath);` Look at the link in my answer. – Win Dec 16 '14 at 20:00
  • i have tried 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) – Art and Artistic artandartisti Dec 16 '14 at 20:07
  • Its not working with IE but works fine with Chrome. Any reason why it is happening? – Art and Artistic artandartisti Dec 16 '14 at 20:08
  • Does it happens in other computers too? If not, your IE setting might be an issue. ***I'm curious whether regular cookie even work in your IE browser.*** Use [this code](http://stackoverflow.com/a/24957931/296861) to test it. – Win Dec 16 '14 at 20:44
0

As far as I remember cookie.Secure must be set correctly, i.e. false for HTTP and true for HTTPS. You can use the Request object to auto-detect the protocol:

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

Hope this helps.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29