Compartir a través de


interface ICoreWebView2CookieManager

interface ICoreWebView2CookieManager
  : 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.
CopyCookie Creates a cookie whose params matches those of the specified cookie.
CreateCookie Create a cookie object with a specified name, value, domain, and path.
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.

Applies to

Product Introduced
WebView2 Win32 1.0.705.50
WebView2 Win32 Prerelease 1.0.721

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(ICoreWebView2Cookie * cookie)

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

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

CopyCookie

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

public HRESULT CopyCookie(ICoreWebView2Cookie * cookieParam, ICoreWebView2Cookie ** cookie)

CreateCookie

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

public HRESULT CreateCookie(LPCWSTR name, LPCWSTR value, LPCWSTR domain, LPCWSTR path, ICoreWebView2Cookie ** 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. Leading or trailing whitespace(s), empty string, and special characters are not allowed for name. See ICoreWebView2Cookie for more details.

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(ICoreWebView2Cookie * cookie)

DeleteCookies

Deletes cookies with matching name and uri.

public HRESULT DeleteCookies(LPCWSTR name, LPCWSTR uri)

Cookie name is required. All cookies with the given name where domain and path match provided URI are deleted.

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, ICoreWebView2GetCookiesCompletedHandler * handler)

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

    if (m_cookieManager)
    {
        CHECK_FAILURE(m_cookieManager->GetCookies(
            uri.c_str(),
            Callback<ICoreWebView2GetCookiesCompletedHandler>(
                [this, uri](HRESULT error_code, ICoreWebView2CookieList* 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 (UINT i = 0; i < cookie_list_size; ++i)
                        {
                            wil::com_ptr<ICoreWebView2Cookie> 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"]";
                    }
                    m_appWindow->AsyncMessageBox(std::move(result), L"GetCookies Result");
                    return S_OK;
                })
                .Get()));
    }