diff --git a/Directory.Packages.props b/Directory.Packages.props index 796af21c..e9307145 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -52,6 +52,6 @@ - + - + \ No newline at end of file diff --git a/src/LinkDotNet.Blog.Web/Features/Bookmarks/BookmarkService.cs b/src/LinkDotNet.Blog.Web/Features/Bookmarks/BookmarkService.cs index 189fc28b..a84e6ab3 100644 --- a/src/LinkDotNet.Blog.Web/Features/Bookmarks/BookmarkService.cs +++ b/src/LinkDotNet.Blog.Web/Features/Bookmarks/BookmarkService.cs @@ -55,7 +55,7 @@ public async Task SetBookmark(string postId, bool isBookmarked) private async Task InitializeIfNotExists() { - if (!(await localStorageService.ContainKeyAsync("bookmarks"))) + if (!(await localStorageService.ContainsKeyAsync("bookmarks"))) { await localStorageService.SetItemAsync("bookmarks", new List()); } diff --git a/src/LinkDotNet.Blog.Web/Features/Home/Components/ThemeToggler.razor b/src/LinkDotNet.Blog.Web/Features/Home/Components/ThemeToggler.razor index ab2f4686..735ee99c 100644 --- a/src/LinkDotNet.Blog.Web/Features/Home/Components/ThemeToggler.razor +++ b/src/LinkDotNet.Blog.Web/Features/Home/Components/ThemeToggler.razor @@ -51,7 +51,7 @@ { await using var _ = await JSRuntime.InvokeAsync("import", "./Features/Home/Components/ThemeToggler.razor.js"); - currentTheme = await LocalStorageService.ContainKeyAsync(StorageKey) + currentTheme = await LocalStorageService.ContainsKeyAsync(StorageKey) ? await LocalStorageService.GetItemAsync(StorageKey) : await JSRuntime.InvokeAsync("getCurrentSystemPreference"); diff --git a/src/LinkDotNet.Blog.Web/Features/Services/ILocalStorageService.cs b/src/LinkDotNet.Blog.Web/Features/Services/ILocalStorageService.cs index 1fd4bf40..ebc12372 100644 --- a/src/LinkDotNet.Blog.Web/Features/Services/ILocalStorageService.cs +++ b/src/LinkDotNet.Blog.Web/Features/Services/ILocalStorageService.cs @@ -4,7 +4,7 @@ namespace LinkDotNet.Blog.Web.Features.Services; public interface ILocalStorageService { - ValueTask ContainKeyAsync(string key); + ValueTask ContainsKeyAsync(string key); ValueTask GetItemAsync(string key); diff --git a/src/LinkDotNet.Blog.Web/Features/Services/LocalStorageService.cs b/src/LinkDotNet.Blog.Web/Features/Services/LocalStorageService.cs index aa24ee8b..f71d7715 100644 --- a/src/LinkDotNet.Blog.Web/Features/Services/LocalStorageService.cs +++ b/src/LinkDotNet.Blog.Web/Features/Services/LocalStorageService.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Security.Cryptography; using System.Threading.Tasks; using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; @@ -13,10 +15,47 @@ public LocalStorageService(ProtectedLocalStorage localStorage) this.localStorage = localStorage; } - public async ValueTask ContainKeyAsync(string key) => (await localStorage.GetAsync(key)).Success; + public async ValueTask ContainsKeyAsync(string key) + { + try + { + return (await localStorage.GetAsync(key)).Success; + } + catch (CryptographicException) + { + await localStorage.DeleteAsync(key); + return false; + } + } - public async ValueTask GetItemAsync(string key) => (await localStorage.GetAsync(key)).Value - ?? throw new KeyNotFoundException($"Key {key} not found"); + public async ValueTask GetItemAsync(string key) + { + try + { + var result = await localStorage.GetAsync(key); + if (!result.Success) + { + throw new KeyNotFoundException($"Key {key} not found"); + } + return result.Value!; + } + catch (CryptographicException) + { + await localStorage.DeleteAsync(key); + throw new KeyNotFoundException($"Key {key} was invalid and has been removed"); + } + } - public async ValueTask SetItemAsync(string key, T value) => await localStorage.SetAsync(key, value!); + public async ValueTask SetItemAsync(string key, T value) + { + try + { + await localStorage.SetAsync(key, value!); + } + catch (CryptographicException) + { + await localStorage.DeleteAsync(key); + throw new InvalidOperationException($"Could not set value for key {key}. The key has been removed."); + } + } } diff --git a/src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Like.razor b/src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Like.razor index 2b69de55..448db8e2 100644 --- a/src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Like.razor +++ b/src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Like.razor @@ -3,7 +3,7 @@ @inject ILocalStorageService LocalStorage