interface ICoreWebView2CookieManager
Note
This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.
interface ICoreWebView2CookieManager
: public IUnknown
Creates, adds or updates, gets, or or view the cookies.
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.
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()));
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. name that starts with whitespace(s) is not allowed. See ICoreWebView2Cookie for more details.
Creates a cookie whose params matches those of the specified cookie.
public HRESULT CreateCookieWithCookie(ICoreWebView2Cookie * cookieParam, ICoreWebView2Cookie ** cookie)
Deletes all cookies under the same profile.
public HRESULT DeleteAllCookies()
This could affect other WebViews under the same user profile.
Deletes a cookie whose name and domain/path pair match those of the specified cookie.
public HRESULT DeleteCookie(ICoreWebView2Cookie * cookie)
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.
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.
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 (int 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"]";
}
MessageBox(nullptr, result.c_str(), L"GetCookies Result", MB_OK);
return S_OK;
})
.Get()));
}