locked
IXMLHTTPRequest2::SetCookie not working for an existing cookie

    Question

  • I have a JavaScript application that is calling into a C++ utility library primarily so I can have some way of clearing cookies after a server logout (WinJS doesn't allow this).  I am attempting to clear the cookies by calling IXMLHTTPRequest2::GetCookie, clearing the value, then calling IXMLHTTPRequest2::SetCookie but my subsequent IXMLHTTPRequest2::Open/Send calls still use the old cookie.  I confirmed that any other changes I make to the cookie like changing the expiration time to make it expired or changing the cookie flags are also discarded.  I also confirmed that if I call GetCookie, SetCookie, then GetCookie again, the old cookie value is returned.  Here is some sample code:

       DWORD numCookies = 0;
       DWORD allCookieFlags = XHR_COOKIE_IS_SECURE | XHR_COOKIE_IS_SESSION | XHR_COOKIE_THIRD_PARTY |
                              XHR_COOKIE_PROMPT_REQUIRED | XHR_COOKIE_EVALUATE_P3P | XHR_COOKIE_APPLY_P3P |
                              XHR_COOKIE_P3P_ENABLED | XHR_COOKIE_IS_RESTRICTED | XHR_COOKIE_IE6 |
                              XHR_COOKIE_IS_LEGACY | XHR_COOKIE_NON_SCRIPT | XHR_COOKIE_HTTPONLY;
       XHR_COOKIE *cookies = NULL;
       HRESULT hr = g_xhr->GetCookie(url->Data(), name->Data(), allCookieFlags,
                                     &numCookies, &cookies);
       if (SUCCEEDED(hr) && numCookies > 0) {
          DWORD cookieState = 0;
          cookies[0].pwszValue = L"";
          hr = g_xhr->SetCookie(cookies, &cookieState);
          CoTaskMemFree(cookies);
          cookies = NULL;
          numCookies = 0;
          hr = g_xhr->GetCookie(url->Data(), name->Data(), allCookieFlags, &numCookies, &cookies);
       }

    For some reason, the last GetCookie call returns the old cookie value.  Is there something I'm doing wrong?  Or is using SetCookie to set an existing cookie not allowed for some reason?

    Friday, November 30, 2012 3:28 PM

All replies