@@ -4,7 +4,7 @@ Cookie management in WebView has been one of the top feature requests. With that
44
55# Description
66
7- One can create a ` CookieManager ` off a WebView to ` GetCookies ` via a ` CookieList ` collection, ` SetCookie ` , ` DeleteCookies ` , and ` ClearAllCookies ` in a WebView environment.
7+ One can get the ` CookieManager ` associated with a WebView to ` GetCookies ` via a ` CookieList ` collection, ` AddOrUpdateCookie ` , ` DeleteCookies ` , and ` DeleteAllCookies ` in a WebView environment.
88
99# Examples
1010
@@ -16,9 +16,9 @@ The following code snippet demonstrates how the cookie management APIs can be us
1616ScenarioCookieManagement::ScenarioCookieManagement (AppWindow* appWindow)
1717 : m_appWindow(appWindow), m_webView(appWindow->GetWebView())
1818{
19- //! [ CreateCookieManager ]
20- CHECK_FAILURE(m_webView->CreateCookieManager (&m_cookieManager));
21- //! [ CreateCookieManager ]
19+ //! [ CookieManager ]
20+ CHECK_FAILURE(m_webViewstaging->get_CookieManager (&m_cookieManager));
21+ //! [ CookieManager ]
2222
2323 CHECK_FAILURE(m_webView->add_WebMessageReceived(
2424 Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
@@ -43,25 +43,25 @@ ScenarioCookieManagement::ScenarioCookieManagement(AppWindow* appWindow)
4343 L"{\"CookiesGot\":\"" + GetCookiesHelper(message.substr(11)) + L"\"}";
4444 CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
4545 }
46- else if (message.compare(0, 10, L"SetCookie ") == 0)
46+ else if (message.compare(0, 10, L"AddOrUpdateCookie ") == 0)
4747 {
4848 message = message.substr(10);
4949 std::wstring name = message.substr(0, message.find(' '));
5050 std::wstring value = message.substr(message.find(' ') + 1);
5151
52- //! [SetCookie ]
52+ //! [AddOrUpdateCookie ]
5353 wil::com_ptr<ICoreWebView2Cookie> cookie;
5454 CHECK_FAILURE(m_cookieManager->CreateCookie(
5555 name.c_str(), value.c_str(), L".bing.com", L"/", &cookie));
56- CHECK_FAILURE(m_cookieManager->SetCookie (cookie.get()));
57- reply = L"{\"CookieSet \":\"Cookie set successfully.\"}";
56+ CHECK_FAILURE(m_cookieManager->AddOrUpdateCookie (cookie.get()));
57+ reply = L"{\"CookieAddedOrUpdated \":\"Cookie added or updated successfully.\"}";
5858 CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
59- //! [SetCookie ]
59+ //! [AddOrUpdateCookie ]
6060 }
6161 else if (message.compare(0, 16, L"ClearAllCookies ") == 0)
6262 {
63- CHECK_FAILURE(m_cookieManager->ClearAllCookies ());
64- reply = L"{\"CookiesCleared \":\"Cookies cleared .\"}";
63+ CHECK_FAILURE(m_cookieManager->DeleteAllCookies ());
64+ reply = L"{\"CookiesDeleted \":\"Cookies all deleted .\"}";
6565 CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
6666 }
6767 return S_OK;
@@ -84,7 +84,7 @@ static std::wstring CookieToJsonString(ICoreWebView2Cookie* cookie)
8484 double expires;
8585 CHECK_FAILURE(cookie->get_Expires(&expires));
8686 BOOL httpOnly;
87- CHECK_FAILURE(cookie->get_HttpOnly (&httpOnly));
87+ CHECK_FAILURE(cookie->get_IsHttpOnly (&httpOnly));
8888 COREWEBVIEW2_COOKIE_SAME_SITE_KIND same_site;
8989 std::wstring same_site_as_string;
9090 CHECK_FAILURE(cookie->get_SameSite(&same_site));
@@ -101,7 +101,7 @@ static std::wstring CookieToJsonString(ICoreWebView2Cookie* cookie)
101101 break;
102102 }
103103 BOOL secure;
104- CHECK_FAILURE(cookie->get_Secure (&secure));
104+ CHECK_FAILURE(cookie->get_IsSecure (&secure));
105105
106106 std::wstring result = L"{";
107107 result += L"\"Name\": " + EncodeQuote(name.get()) + L", " + L"\"Value\": " +
@@ -174,39 +174,36 @@ void ScenarioCookieManagement::GetCookiesHelper(std::wstring uri)
174174## .NET and WinRT
175175
176176```c#
177- CoreWebView2CookieManager _cookieManager;
178-
179- private void WebView_CoreWebView2Ready(object sender, EventArgs e)
177+ void CookieManagementCmdsCanExecute(object sender, CanExecuteRoutedEventArgs e)
180178{
181- _cookieManager = webView.CoreWebView2.CreateCookieManager() ;
179+ e.CanExecute = webView != null && webView .CoreWebView2 != null && webView.CoreWebView2.CookieManager != null ;
182180}
183181
184- void SetCookieCmdExecuted (object target, ExecutedRoutedEventArgs e)
182+ void AddOrUpdateCookieCmdExecuted (object target, ExecutedRoutedEventArgs e)
185183{
186- CoreWebView2Cookie cookie = _cookieManager .CreateCookie("CookieName", "CookieValue", ".bing.com", "/");
184+ CoreWebView2Cookie cookie = webView.CoreWebView2.CookieManager .CreateCookie("CookieName", "CookieValue", ".bing.com", "/");
187185 cookie.SameSite = CoreWebView2CookieSameSiteKind.None;
188- _cookieManager.SetCookie (cookie);
186+ webView.CoreWebView2.CookieManager.AddOrUpdateCookie (cookie);
189187}
190188
191189async void GetCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e)
192190{
193- CoreWebView2CookieList cookieList = await _cookieManager .GetCookiesAsync("https://www.bing.com");
191+ CoreWebView2CookieList cookieList = await webView.CoreWebView2.CookieManager .GetCookiesAsync("https://www.bing.com");
194192 for (uint i = 0; i < cookieList.Size; ++i)
195193 {
196194 CoreWebView2Cookie cookie = cookieList.GetValueAtIndex(i);
197195 Console.WriteLine(CookieAsString(cookie));
198196 }
199-
200197}
201198
202- void ClearAllCookiesCmdExecuted (object target, ExecutedRoutedEventArgs e)
199+ void DeleteAllCookiesCmdExecuted (object target, ExecutedRoutedEventArgs e)
203200{
204- _cookieManager.ClearAllCookies ();
201+ webView.CoreWebView2.CookieManager.DeleteAllCookies ();
205202}
206203
207204void DeleteCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e)
208205{
209- _cookieManager .DeleteCookies("CookieName", "https://www.bing.com", "", "");
206+ webView.CoreWebView2.CookieManager .DeleteCookies("CookieName", "https://www.bing.com", "", "");
210207}
211208```
212209
@@ -240,8 +237,8 @@ typedef enum COREWEBVIEW2_COOKIE_SAME_SITE_KIND {
240237
241238[uuid(20113081-93BD-4F2A-86B9-ADF92DEAAF10), object, pointer_default(unique)]
242239interface ICoreWebView2 : IUnknown {
243- /// Create a new cookie manager object. See ICoreWebView2CookieManager .
244- HRESULT CreateCookieManager ([out, retval] ICoreWebView2CookieManager** cookieManager);
240+ /// Gets the cookie manager object associated with this ICoreWebView2 .
241+ [propget] HRESULT CookieManager ([out, retval] ICoreWebView2CookieManager** cookieManager);
245242}
246243
247244/// Provides a set of properties that are used to manage an
@@ -274,9 +271,9 @@ interface ICoreWebView2Cookie : IUnknown {
274271 /// Whether this cookie is http-only.
275272 /// True if a page script or other active content cannot access this
276273 /// cookie. The default is false.
277- [propget] HRESULT HttpOnly ([out, retval] BOOL* httpOnly);
274+ [propget] HRESULT IsHttpOnly ([out, retval] BOOL* httpOnly);
278275 /// Set the HttpOnly property.
279- [propput] HRESULT HttpOnly ([in] BOOL httpOnly);
276+ [propput] HRESULT IsHttpOnly ([in] BOOL httpOnly);
280277
281278 /// SameSite status of the cookie which represents the enforcement mode of the cookie.
282279 /// The default is COREWEBVIEW2_COOKIE_SAME_SITE_KIND_LAX.
@@ -289,18 +286,20 @@ interface ICoreWebView2Cookie : IUnknown {
289286 /// The default is false.
290287 /// Note that cookie that requests COREWEBVIEW2_COOKIE_SAME_SITE_KIND_NONE but
291288 /// is not marked Secure will be rejected.
292- [propget] HRESULT Secure ([out, retval] BOOL* secure);
289+ [propget] HRESULT IsSecure ([out, retval] BOOL* secure);
293290 /// Set the Secure property.
294- [propput] HRESULT Secure ([in] BOOL secure);
291+ [propput] HRESULT IsSecure ([in] BOOL secure);
295292}
296293
297- /// Add or delete an ICoreWebView2Cookie or ICoreWebView2Cookies,
298- /// or view the cookies. The changes would apply to the context of the user profile.
299- /// That is, other WebViews under the same user profile could be affected.
294+ /// Creates, adds or updates, gets, or or view the cookies. The changes would
295+ /// apply to the context of the user profile. That is, other WebViews under the
296+ /// same user profile could be affected.
300297[uuid(588C8A15-A28A-4FFD-926B-5E6EE7449E7C), object, pointer_default(unique)]
301298interface ICoreWebView2CookieManager : IUnknown {
302299 /// Create a cookie object with a specified name, value, domain, and path.
303300 /// One can set other optional properties after cookie creation.
301+ /// This only creates a cookie object and it is not added to the cookie
302+ /// manager until you call AddOrUpdateCookie.
304303 /// See ICoreWebView2Cookie for more details.
305304 HRESULT CreateCookie(
306305 [in] LPCWSTR name,
@@ -311,15 +310,15 @@ interface ICoreWebView2CookieManager : IUnknown {
311310
312311 /// Gets a list of cookies matching the specific URI.
313312 /// You can modify the cookie objects, call
314- /// ICoreWebView2CookieManager::SetCookie , and the changes
313+ /// ICoreWebView2CookieManager::AddOrUpdateCookie , and the changes
315314 /// will be applied to the webview.
316315 HRESULT GetCookies(
317316 [in] LPCWSTR uri,
318317 [in] ICoreWebView2GetCookiesCompletedHandler* handler);
319318
320- /// Sets a cookie with the given cookie data; may overwrite equivalent cookies
321- /// if they exist.
322- HRESULT SetCookie ([in] ICoreWebView2Cookie* cookie);
319+ /// Adds or updates a cookie with the given cookie data; may overwrite
320+ /// equivalent cookies if they exist.
321+ HRESULT AddOrUpdateCookie ([in] ICoreWebView2Cookie* cookie);
323322
324323 /// Deletes browser cookies with matching name and uri or domain/path pair.
325324 /// Cookie name is required.
@@ -329,9 +328,9 @@ interface ICoreWebView2CookieManager : IUnknown {
329328 /// If path is specified, deletes only cookies with the exact path.
330329 HRESULT DeleteCookies([in] LPCWSTR name, [in] LPCWSTR uri, [in] LPCWSTR domain, [in] LPCWSTR path);
331330
332- /// Clears all cookies under the same profile.
331+ /// Deletes all cookies under the same profile.
333332 /// This could affect other WebViews under the same user profile.
334- HRESULT ClearAllCookies ();
333+ HRESULT DeleteAllCookies ();
335334}
336335
337336/// A list of cookie objects. See ICoreWebView2Cookie.
@@ -360,6 +359,8 @@ interface ICoreWebView2GetCookiesCompletedHandler : IUnknown {
360359``` c#
361360namespace Microsoft .Web .WebView2 .Core
362361{
362+ // ...
363+
363364 /// Kind of cookie SameSite status used in the CoreWebView2Cookie class.
364365 /// These fields match those as specified in https://developer.mozilla.org/docs/Web/HTTP/Cookies#.
365366 /// Learn more about SameSite cookies here: https://tools.ietf.org/html/draft-west-first-party-cookies-07
@@ -375,28 +376,32 @@ namespace Microsoft.Web.WebView2.Core
375376
376377 runtimeclass CoreWebView2
377378 {
378- /// Create a new cookie manager object.
379- CoreWebView2CookieManager CreateCookieManager();
379+ /// Gets the cookie manager object associated with this CoreWebView2.
380+ CoreWebView2CookieManager CookieManager { get; };
381+
382+ // ...
380383 }
381384
382- /// Add or delete an CoreWebView2Cookie or CoreWebView2Cookies,
383- /// or view the cookies. The changes would apply to the context of the user profile.
384- /// That is, other WebViews under the same user profile could be affected.
385+ /// Creates, adds or updates, gets, or or view the cookies. The changes would
386+ /// apply to the context of the user profile. That is, other WebViews under the
387+ /// same user profile could be affected.
385388 runtimeclass CoreWebView2CookieManager
386389 {
387390 /// Create a cookie object with a specified name, value, domain, and path.
388391 /// One can set other optional properties after cookie creation.
392+ /// This only creates a cookie object and it is not added to the cookie
393+ /// manager until you call AddOrUpdateCookie.
389394 CoreWebView2Cookie CreateCookie (String name , String value , String Domain , String Path );
390395
391396 /// Gets a list of cookies matching the specific URI.
392397 /// You can modify the cookie objects, call
393- /// CoreWebView2CookieManager.SetCookie , and the changes
398+ /// CoreWebView2CookieManager.AddOrUpdateCookie , and the changes
394399 /// will be applied to the webview.
395400 Windows .Foundation .IAsyncOperation < CoreWebView2CookieList > GetCookiesAsync (String uri );
396401
397- /// Sets a cookie with the given cookie data; may overwrite equivalent cookies
398- /// if they exist.
399- void SetCookie (CoreWebView2Cookie cookie );
402+ /// Adds or updates a cookie with the given cookie data; may overwrite
403+ /// equivalent cookies if they exist.
404+ void AddOrUpdateCookie (CoreWebView2Cookie cookie );
400405
401406 /// Deletes browser cookies with matching name and uri or domain/path pair.
402407 /// Cookie name is required.
@@ -406,9 +411,9 @@ namespace Microsoft.Web.WebView2.Core
406411 /// If path is specified, deletes only cookies with the exact path.
407412 void DeleteCookies (String name , String uri , String Domain , String Path );
408413
409- /// Clears all cookies under the same profile.
414+ /// Deletes all cookies under the same profile.
410415 /// This could affect other WebViews under the same user profile.
411- void ClearAllCookies ();
416+ void DeleteAllCookies ();
412417 }
413418
414419 /// A list of cookie objects.
@@ -445,7 +450,7 @@ namespace Microsoft.Web.WebView2.Core
445450 /// Whether this cookie is http-only.
446451 /// True if a page script or other active content cannot access this
447452 /// cookie. The default is false.
448- Int32 HttpOnly { get ; set ; };
453+ Boolean IsHttpOnly { get ; set ; };
449454
450455 /// SameSite status of the cookie which represents the enforcement mode of the cookie.
451456 /// The default is CoreWebView2CookieSameSiteKind.Lax.
@@ -456,7 +461,9 @@ namespace Microsoft.Web.WebView2.Core
456461 /// The default is false.
457462 /// Note that cookie that requests CoreWebView2CookieSameSiteKind.None but
458463 /// is not marked Secure will be rejected.
459- Int32 Secure { get ; set ; };
464+ Boolean IsSecure { get ; set ; };
460465 }
466+
467+ // ...
461468 }
462469```
0 commit comments