-
Notifications
You must be signed in to change notification settings - Fork 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- A Toolkit class to easily work with cookies in the browser (#5431)
- Loading branch information
1 parent
78f96e0
commit b4d18a5
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#if __WASM__ | ||
#nullable enable | ||
|
||
namespace Uno.UI.Toolkit.Web | ||
{ | ||
public class Cookie | ||
{ | ||
public Cookie(string name, string value) | ||
{ | ||
Name = name ?? throw new System.ArgumentNullException(nameof(name)); | ||
Value = value ?? throw new System.ArgumentNullException(nameof(value)); | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public string Value { get; } | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#if __WASM__ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Uno.Foundation; | ||
|
||
namespace Uno.UI.Toolkit.Web | ||
{ | ||
public class CookieManager | ||
{ | ||
private const char ValueSeparator = '='; | ||
private const char AttributeSeparator = ';'; | ||
|
||
private static readonly Lazy<CookieManager> _cookieManager = new Lazy<CookieManager>(() => new CookieManager()); | ||
|
||
private CookieManager() | ||
{ | ||
} | ||
|
||
public static CookieManager GetDefault() => _cookieManager.Value; | ||
|
||
public void SetCookie(SetCookieRequest cookie) | ||
{ | ||
var builder = new StringBuilder(); | ||
builder.Append(Uri.EscapeDataString(cookie.Name)); | ||
builder.Append(ValueSeparator); | ||
builder.Append(Uri.EscapeDataString(cookie.Value)); | ||
|
||
static void AppendAttribute(StringBuilder builder, string name, object? value) | ||
{ | ||
if (value == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(AttributeSeparator); | ||
builder.Append(name); | ||
builder.Append(ValueSeparator); | ||
builder.Append(value); | ||
} | ||
|
||
AppendAttribute(builder, "path", cookie.Path); | ||
AppendAttribute(builder, "domain", cookie.Domain); | ||
AppendAttribute(builder, "max-age", cookie.MaxAge?.ToString()); | ||
AppendAttribute(builder, "expires", cookie.Expires?.ToString("r")); | ||
AppendAttribute(builder, "samesite", cookie.SameSite?.ToString("g")?.ToLowerInvariant()); | ||
if (cookie.Secure) | ||
{ | ||
builder.Append(AttributeSeparator); | ||
builder.Append("secure"); | ||
} | ||
|
||
var escapedCookie = WebAssemblyRuntime.EscapeJs(builder.ToString()); | ||
var jsInvoke = $"document.cookie = '{escapedCookie}'"; | ||
WebAssemblyRuntime.InvokeJS(jsInvoke); | ||
} | ||
|
||
public void DeleteCookie(string name, string? path = null) | ||
{ | ||
var setCookieRequest = new SetCookieRequest(name, string.Empty) | ||
{ | ||
Expires = DateTimeOffset.MinValue, | ||
Path = path | ||
}; | ||
|
||
SetCookie(setCookieRequest); | ||
} | ||
|
||
public Cookie? GetCookie(string name) => GetCookies().FirstOrDefault(c => c.Name == name); | ||
|
||
public Cookie[] GetCookies() | ||
{ | ||
Cookie ParseCookie(string cookieString) | ||
{ | ||
var cookieParts = cookieString.Split("="); | ||
var name = Uri.UnescapeDataString(cookieParts[0]); | ||
var value = Uri.UnescapeDataString(cookieParts[1]); | ||
return new Cookie(name, value); | ||
} | ||
|
||
var cookies = WebAssemblyRuntime.InvokeJS("document.cookie"); | ||
if (string.IsNullOrWhiteSpace(cookies)) | ||
{ | ||
return Array.Empty<Cookie>(); | ||
} | ||
|
||
var cookieStrings = cookies.Split(";", StringSplitOptions.RemoveEmptyEntries); | ||
return cookieStrings.Select(part => ParseCookie(part)).ToArray(); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#if __WASM__ | ||
namespace Uno.UI.Toolkit.Web | ||
{ | ||
public enum CookieSameSite | ||
{ | ||
Lax, | ||
Strict, | ||
None | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#if __WASM__ | ||
#nullable enable | ||
|
||
|
||
using System; | ||
|
||
namespace Uno.UI.Toolkit.Web | ||
{ | ||
public class SetCookieRequest | ||
{ | ||
public SetCookieRequest(string name, string value) | ||
{ | ||
Name = name; | ||
Value = value; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public string Value { get; } | ||
|
||
public string? Path { get; set; } | ||
|
||
public string? Domain { get; set; } | ||
|
||
public int? MaxAge { get; set; } | ||
|
||
public DateTimeOffset? Expires { get; set; } | ||
|
||
public bool Secure { get; set; } | ||
|
||
public CookieSameSite? SameSite { get; set; } | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters