@@ -22,6 +22,13 @@ multiple WebView2s running with separate profiles under a single user data direc
2222browser instance at runtime), which means separate cookies, user preference settings, and various
2323data storage etc., to help you build a more wonderful experience for your application.
2424
25+ Providing the CookieManager from the profile is more logical, and it sets the groundwork for allowing
26+ an app to manage the cookies of a profile without having to create a WebView2 first. In order to
27+ manage cookies through the profile, we're adding a get_CookieManager interface into the profile.
28+ Users can use this interface to get the cookie manager, which is shared by all WebView2s associated
29+ with this profile. The cookie manager got from profile (CoreWebView2.Profile.CookieManager) is the
30+ same as that got from CoreWebView2 (CoreWebView2.CookieManager).
31+
2532# Examples
2633
2734## Win32 C++
@@ -118,6 +125,46 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
118125 // ...
119126}
120127```
128+
129+ ### Access and use cookie manager from profile
130+
131+ ```cpp
132+ wil::com_ptr<ICoreWebView2CookieManager> m_cookieManager;
133+ ScenarioCookieManagement::ScenarioCookieManagement(ICoreWebView2Controller* controller)
134+ {
135+ wil::com_ptr<ICoreWebView2> coreWebView2;
136+ CHECK_FAILURE(controller->get_CoreWebView2(&coreWebView2));
137+ auto webview7 = coreWebView2.try_query<ICoreWebView2_7>();
138+ if (webview7)
139+ {
140+ wil::com_ptr<ICoreWebView2Profile> profile;
141+ CHECK_FAILURE(webview7->get_Profile(&profile));
142+ auto profile2 = profile.try_query<ICoreWebView2Profile2>;
143+ if (profile2)
144+ {
145+ CHECK_FAILURE(profile2->get_CookieManager(&m_cookieManager));
146+ }
147+ }
148+ // ...
149+ }
150+
151+ // Use cookie manager to add or update a cookie
152+ void ScenarioCookieManagement::AddOrUpdateCookie(const std::wstring& name, const std::wstring& value, const std::wstring& domain)
153+ {
154+ CHECK(m_cookieManager);
155+ wil::com_ptr<ICoreWebView2Cookie> cookie;
156+ CHECK_FAILURE(m_cookieManager->CreateCookie(
157+ name.c_str(), value.c_str(), domain.c_str(), L"/", &cookie));
158+ CHECK_FAILURE(m_cookieManager->AddOrUpdateCookie(cookie.get()));
159+ }
160+
161+ // Use cookie manager to delete all cookies
162+ void ScenarioCookieManagement::DeleteAllCookies()
163+ {
164+ CHECK(m_cookieManager);
165+ CHECK_FAILURE(m_cookieManager->DeleteAllCookies();
166+ }
167+ ```
121168## .NET and WinRT
122169
123170### Create WebView2 with a specific profile, then access the profile property of WebView2
@@ -153,6 +200,32 @@ public CreateWebView2Controller(IntPtr parentWindow)
153200}
154201```
155202
203+ ### Access and use the cookie manager from profile.
204+
205+ ``` csharp
206+ CoreWebView2CookieManager _cookieManager ;
207+ public ScenarioCookieManagement (CoreWebView2Controller controller ){
208+ // get the cookie manager from controller
209+ _cookieManager = controller .CoreWebView2 .Profile .CookieManager ;
210+ // ...
211+ }
212+
213+ // Use cookie manager to add or update a cookie
214+ public AddOrUpdateCookie (string name , string value , string Domain )
215+ {
216+ // create cookie with given parameters and default path
217+ CoreWebView2Cookie cookie = cookieManager .CreateCookie (name , value , Domain , " /" );
218+ // add or update cookie
219+ _cookieManager .AddOrUpdateCookie (cookie );
220+ }
221+
222+ // Use cookie manager to delete all cookies
223+ void DeleteAllCookies ()
224+ {
225+ _cookieManager .DeleteAllCookies ();
226+ }
227+ ```
228+
156229# API Details
157230
158231## Win32 C++
@@ -162,6 +235,7 @@ interface ICoreWebView2ControllerOptions;
162235interface ICoreWebView2Environment5;
163236interface ICoreWebView2_7;
164237interface ICoreWebView2Profile;
238+ interface ICoreWebView2Profile2;
165239
166240/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
167241[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -247,6 +321,13 @@ interface ICoreWebView2Profile : IUnknown {
247321
248322 // TODO: All profile-wide operations/settings will be put below in the future.
249323}
324+
325+ [uuid(B93875C2-D6B0-434D-A2BE-93BC06CCC469), object, pointer_default(unique)]
326+ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
327+ /// Get the cookie manager object for the profile. All CoreWebView2s associated with this profile share this same cookie manager and will have the same CoreWebView2.CookieManager property value.
328+ /// See ICoreWebView2CookieManager.
329+ [propget] HRESULT CookieManager([out, retval] ICoreWebView2CookieManager** cookieManager);
330+ }
250331```
251332
252333## .NET and WinRT
@@ -296,6 +377,8 @@ namespace Microsoft.Web.WebView2.Core
296377 Boolean IsInPrivateModeEnabled { get ; };
297378
298379 String ProfilePath { get ; };
380+
381+ CoreWebView2CookieManager CookieManager { get ; };
299382 }
300383}
301384```
0 commit comments