Skip to content

Commit 4dcd7b4

Browse files
committed
Review feedback 3: Applied recommended scenarios as .NET sample code
1 parent bb8f31b commit 4dcd7b4

1 file changed

Lines changed: 65 additions & 16 deletions

File tree

specs/CookieManagement.md

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,33 +179,82 @@ void ScenarioCookieManagement::GetCookiesHelper(std::wstring uri)
179179
180180
## .NET and WinRT
181181
182-
```c#
183-
void AddOrUpdateCookieCmdExecuted(object target, ExecutedRoutedEventArgs e)
182+
### Scenario 1
183+
184+
In this scenario, the host application reads contoso.com cookies from the WebView to see if the user is logged in.
185+
186+
```csharp
187+
Task<bool> IsUserSignedInToContoso()
184188
{
185-
CoreWebView2Cookie cookie = webView.CoreWebView2.CookieManager.CreateCookie("CookieName", "CookieValue", ".bing.com", "/");
186-
cookie.SameSite = CoreWebView2CookieSameSiteKind.None;
187-
webView.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);
189+
CoreWebView2CookieManager cookieManager = webView.CoreWebView2.CookieManager;
190+
List<CoreWebView2Cookie> cookieList =
191+
await webView.CoreWebView2.CookieManager.GetCookiesAsync("https://contoso.com");
192+
foreach (CoreWebView2Cookie cookie in cookieList)
193+
{
194+
if (cookie.Name == "is_signed_in")
195+
{
196+
return cookie.Value == "1";
197+
}
198+
}
199+
return false;
188200
}
201+
```
202+
203+
### Scenario 2
204+
205+
In this scenario, the host application sets the remembered user ID, so that when the user goes to contoso, their userid is pre-filled by the site.
189206

190-
async void GetCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e)
207+
```csharp
208+
void SetRememberedUserId(string id)
191209
{
192-
IList<CoreWebView2Cookie> cookieList = await webView.CoreWebView2.CookieManager.GetCookiesAsync("https://www.bing.com");
193-
for (uint i = 0; i < cookieList.Count; ++i)
210+
CoreWebView2CookieManager cookieManager = webView.CoreWebView2.CookieManager;
211+
CoreWebView2Cookie cookie =
212+
cookieManager.CreateCookieWithDetails(
213+
"last_userid", id, "contoso.com", "/");
214+
cookie.IsSecure = true; // disallow http://contoso.com/
215+
cookieManager.AddOrUpdateCookie(cookie);
216+
}
217+
```
218+
219+
### Scenario 3
220+
221+
In this scenario, the host application clears the remembered user ID because the user signed out of the host app.
222+
223+
```csharp
224+
void ClearRememberedUserId()
194225
{
195-
CoreWebView2Cookie cookie = cookieList[i];
196-
Cookie systemNetCookie = cookie.ToSystemNetCookie();
197-
Console.WriteLine(systemNetCookie.ToString());
198-
}
226+
CoreWebView2CookieManager cookieManager = webView.CoreWebView2.CookieManager;
227+
cookieManager.DeleteCookiesWithNameAndPath("last_userid", "https://contoso.com", "/");
199228
}
229+
```
230+
231+
### Scenario 4
232+
233+
In this scenario, the host application clears all cookies not belonging to contoso. Note that `List<CoreWebView2Cookie>` returned by the `GetCookiesAsync` call is just a snapshot of the cookies and one should call `DeleteCookies` to remove cookies.
200234

201-
void DeleteAllCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e)
235+
```csharp
236+
Task ClearNonContosoCookies()
202237
{
203-
webView.CoreWebView2.CookieManager.DeleteAllCookies();
238+
CoreWebView2CookieManager cookieManager = webView.CoreWebView2.CookieManager;
239+
List<CoreWebView2Cookie> cookieList =
240+
await webView.CoreWebView2.CookieManager.GetCookiesAsync(null);
241+
foreach (CoreWebView2Cookie cookie in cookieList)
242+
{
243+
if (cookie.Domain != "contoso.com") cookieManager.DeleteCookie(cookie);
244+
}
204245
}
246+
```
205247

206-
void DeleteCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e)
248+
### Scenario 5
249+
250+
In this scenario, the host application clears all cookies as part of a troubleshooter.
251+
252+
```csharp
253+
void ClearCookiesTroubleshooter()
207254
{
208-
webView.CoreWebView2.CookieManager.DeleteCookies("CookieName", "https://www.bing.com");
255+
CoreWebView2CookieManager cookieManager = webView.CoreWebView2.CookieManager;
256+
cookieManager.DeleteAllCookies();
257+
209258
}
210259
```
211260

0 commit comments

Comments
 (0)