interface ICoreWebView2ExperimentalCookieManager

Note

This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.

Note

This an experimental API that is shipped with our prerelease SDK. See WebView2 release notes.

interface ICoreWebView2ExperimentalCookieManager
  : public IUnknown

Creates, adds or updates, gets, or or view the cookies.

Summary

Members Descriptions
AddOrUpdateCookie Adds or updates a cookie with the given cookie data; may overwrite cookies with matching name, domain, and path if they exist.
CreateCookie Create a cookie object with a specified name, value, domain, and path.
CreateCookieWithCookie Creates a cookie whose params matches those of the specified cookie.
DeleteAllCookies Deletes all cookies under the same profile.
DeleteCookie Deletes a cookie whose name and domain/path pair match those of the specified cookie.
DeleteCookies Deletes cookies with matching name and uri.
DeleteCookiesWithDomainAndPath Deletes cookies with matching name and domain/path pair.
GetCookies Gets a list of cookies matching the specific URI.

The changes would apply to the context of the user profile. That is, other WebViews under the same user profile could be affected.

Members

AddOrUpdateCookie

Adds or updates a cookie with the given cookie data; may overwrite cookies with matching name, domain, and path if they exist.

public HRESULT AddOrUpdateCookie(ICoreWebView2ExperimentalCookie * cookie)

This method will fail if the domain of the given cookie is not specified.

                    wil::com_ptr<ICoreWebView2ExperimentalCookie> cookie;
                    CHECK_FAILURE(m_cookieManager->CreateCookie(
                        L"CookieName", L"CookieValue", L".bing.com", L"/", &cookie));
                    CHECK_FAILURE(m_cookieManager->AddOrUpdateCookie(cookie.get()));

CreateCookie

Create a cookie object with a specified name, value, domain, and path.

public HRESULT CreateCookie(LPCWSTR name, LPCWSTR value, LPCWSTR domain, LPCWSTR path, ICoreWebView2ExperimentalCookie ** cookie)

One can set other optional properties after cookie creation. This only creates a cookie object and it is not added to the cookie manager until you call AddOrUpdateCookie. name that starts with whitespace(s) is not allowed. See ICoreWebView2ExperimentalCookie for more details.

CreateCookieWithCookie

Creates a cookie whose params matches those of the specified cookie.

public HRESULT CreateCookieWithCookie(ICoreWebView2ExperimentalCookie * cookieParam, ICoreWebView2ExperimentalCookie ** cookie)

DeleteAllCookies

Deletes all cookies under the same profile.

public HRESULT DeleteAllCookies()

This could affect other WebViews under the same user profile.

DeleteCookie

Deletes a cookie whose name and domain/path pair match those of the specified cookie.

public HRESULT DeleteCookie(ICoreWebView2ExperimentalCookie * cookie)

DeleteCookies

Deletes cookies with matching name and uri.

public HRESULT DeleteCookies(LPCWSTR name, LPCWSTR uri)

Cookie name is required. If uri is specified, deletes all cookies with the given name where domain and path match provided URI.

DeleteCookiesWithDomainAndPath

Deletes cookies with matching name and domain/path pair.

public HRESULT DeleteCookiesWithDomainAndPath(LPCWSTR name, LPCWSTR domain, LPCWSTR path)

Cookie name is required. If domain is specified, deletes only cookies with the exact domain. If path is specified, deletes only cookies with the exact path.

GetCookies

Gets a list of cookies matching the specific URI.

public HRESULT GetCookies(LPCWSTR uri, ICoreWebView2ExperimentalGetCookiesCompletedHandler * handler)

If uri is empty string or null, all cookies under the same profile are returned. You can modify the cookie objects by calling ICoreWebView2ExperimentalCookieManager::AddOrUpdateCookie, and the changes will be applied to the webview.

    if (m_cookieManager)
    {
        CHECK_FAILURE(m_cookieManager->GetCookies(
            uri.c_str(),
            Callback<ICoreWebView2ExperimentalGetCookiesCompletedHandler>(
                [this, uri](HRESULT error_code, ICoreWebView2ExperimentalCookieList* list) -> HRESULT {
                    CHECK_FAILURE(error_code);

                    std::wstring result;
                    UINT cookie_list_size;
                    CHECK_FAILURE(list->get_Count(&cookie_list_size));

                    if (cookie_list_size == 0)
                    {
                        result += L"No cookies found.";
                    }
                    else
                    {
                        result += std::to_wstring(cookie_list_size) + L" cookie(s) found";
                        if (!uri.empty())
                        {
                            result += L" on " + uri;
                        }
                        result += L"\n\n[";
                        for (int i = 0; i < cookie_list_size; ++i)
                        {
                            wil::com_ptr<ICoreWebView2ExperimentalCookie> cookie;
                            CHECK_FAILURE(list->GetValueAtIndex(i, &cookie));

                            if (cookie.get())
                            {
                                result += CookieToString(cookie.get());
                                if (i != cookie_list_size - 1)
                                {
                                    result += L",\n";
                                }
                            }
                        }
                        result += L"]";
                    }
                    MessageBox(nullptr, result.c_str(), L"GetCookies Result", MB_OK);
                    return S_OK;
                })
                .Get()));
    }