From f5e7d8e2bd04d2cf1994fb501b3d3d66010f7842 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Tue, 24 Sep 2024 20:05:48 +0200 Subject: [PATCH] fix browser storage --- .../TokensService.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Tokens/TokensService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Tokens/TokensService.cs index 6e02125..f80f8f0 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Tokens/TokensService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Tokens/TokensService.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; +using System.Security.Cryptography; +using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; +using Microsoft.Extensions.Logging; using WatchIt.Common.Model.Accounts; using WatchIt.Website.Services.Utility.Configuration; @@ -8,6 +10,8 @@ public class TokensService : ITokensService { #region SERVICES + private readonly ILogger _logger; + private readonly ProtectedLocalStorage _localStorageService; private readonly IConfigurationService _configurationService; @@ -17,8 +21,10 @@ public class TokensService : ITokensService #region CONSTRUCTORS - public TokensService(ProtectedLocalStorage localStorageService, IConfigurationService configurationService) + public TokensService(ILogger logger, ProtectedLocalStorage localStorageService, IConfigurationService configurationService) { + _logger = logger; + _localStorageService = localStorageService; _configurationService = configurationService; } @@ -56,8 +62,16 @@ public class TokensService : ITokensService private async Task GetValueAsync(string key) { - ProtectedBrowserStorageResult result = await _localStorageService.GetAsync(key); - return result.Success ? result.Value : default; + try + { + ProtectedBrowserStorageResult result = await _localStorageService.GetAsync(key); + return result.Success ? result.Value : default; + } + catch (CryptographicException ex) + { + _logger.LogError(ex, "Browser storage error has occurred. Deleting value."); + await _localStorageService.DeleteAsync(key); + } } #endregion