4

I am creating a model,serializing & assigning into cookie and passing it to next Page. I am able to get cookie values in next page in all browser except

  • MAC -Yoshemite - Safari
  • IOS - IPHONE 6 Mobile- Safari

    Do I need to update the below code to work in Safari.

    string CookieName= "dsResponse";
    string json = new JavaScriptSerializer().Serialize(model);    
    
    if (HttpContext.Current.Request.Cookies[CookieName] != null)
    {
        HttpContext.Current.Response.Cookies[CookieName].Expires = DateTime.Now.AddDays(-1);
    }
    HttpContext.Current.Response.SetCookie(new HttpCookie(CookieName)
    {
        Value = json,
        HttpOnly = false,
        Expires = DateTime.Now.AddSeconds(Convert.ToInt32(ConfigurationManager.AppSettings["cookiesecond"]))
    });
    
Julian
  • 33,915
  • 22
  • 119
  • 174
Shailesh
  • 554
  • 1
  • 6
  • 29
  • How long is the string you tried to save in a cookie? Cookies have a limited space to save data. – F.Igor Apr 07 '17 at 18:21
  • But same thing working in others browser – Shailesh Apr 08 '17 at 10:37
  • 1
    How big is the size of your cookie ? Safari in particular requires to use slim cookies - 4093B per domain is a limit; see http://stackoverflow.com/questions/640938/what-is-the-maximum-size-of-a-web-browsers-cookies-key – Ondrej Svejdar Apr 10 '17 at 09:07
  • is this a 3rd party cookie? Those are blocked by default in Safari – Julian Apr 16 '17 at 16:40

3 Answers3

0

1st: you are overwriting the cookie - not expiring it with this code. The response object is sent once - with your "new" cookie. If the cookie exists - just change its value and/or the content. I would check your assumption on AppSettings["cookiesecond"]

Also try this:

If Request.ServerVariables("http_user_agent").IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) <> -1 Then
    Me.Page.ClientTarget = "uplevel"

It may be the browser caps not matching...

davidWazy
  • 61
  • 6
0

There was some comma was coming in JSON data and its breaking at the time of deserilze the data.

var cookieValue = (json).Replace(";", "").Replace(",", "***");
 if (HttpContext.Current.Request.Browser.Type.ToLower().Contains("safari"))
            {
                HttpContext.Current.Response.AddHeader("Set-Cookie", sessionName + "=" + cookieValue + "; path=/;");
            }
Shailesh
  • 554
  • 1
  • 6
  • 29
0

By default cookie not allowed for iOS safari browser. We have to enable cookies setting from iOS safari browser,

Solution :- -we have implemented local storage(java script concept)to overcome the cookie problems in iOS safari browser.

sugansoft
  • 1,227
  • 8
  • 26