-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store non-sensitive UI state without protection (#5434)
- Loading branch information
Showing
11 changed files
with
254 additions
and
49 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
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
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
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
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
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Dashboard.Model.BrowserStorage; | ||
|
||
public interface ILocalStorage : IBrowserStorage | ||
{ | ||
/// <summary> | ||
/// Get unprotected data from local storage. This must only be used with non-sensitive data. | ||
/// </summary> | ||
Task<StorageResult<T>> GetUnprotectedAsync<T>(string key); | ||
|
||
/// <summary> | ||
/// Set unprotected data to local storage. This must only be used with non-sensitive data. | ||
/// </summary> | ||
Task SetUnprotectedAsync<T>(string key, T value); | ||
} |
51 changes: 49 additions & 2 deletions
51
src/Aspire.Dashboard/Model/BrowserStorage/LocalBrowserStorage.cs
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 |
---|---|---|
@@ -1,13 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Aspire.Dashboard.Model.BrowserStorage; | ||
|
||
public class LocalBrowserStorage : BrowserStorageBase, ILocalStorage | ||
{ | ||
public LocalBrowserStorage(ProtectedLocalStorage protectedLocalStorage) : base(protectedLocalStorage) | ||
private static readonly JsonSerializerOptions s_options = new() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
PropertyNameCaseInsensitive = true, | ||
}; | ||
|
||
private readonly IJSRuntime _jsRuntime; | ||
private readonly ILogger<LocalBrowserStorage> _logger; | ||
|
||
public LocalBrowserStorage(IJSRuntime jsRuntime, ProtectedLocalStorage protectedLocalStorage, ILogger<LocalBrowserStorage> logger) : base(protectedLocalStorage) | ||
{ | ||
_jsRuntime = jsRuntime; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<StorageResult<T>> GetUnprotectedAsync<T>(string key) | ||
{ | ||
var json = await GetJsonAsync(key).ConfigureAwait(false); | ||
|
||
if (json == null) | ||
{ | ||
return new StorageResult<T>(false, default); | ||
} | ||
|
||
try | ||
{ | ||
return new StorageResult<T>(true, JsonSerializer.Deserialize<T>(json, s_options)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning(ex, $"Error when reading '{key}' as {typeof(T).Name} from local browser storage."); | ||
|
||
return new StorageResult<T>(false, default); | ||
} | ||
} | ||
|
||
public async Task SetUnprotectedAsync<T>(string key, T value) | ||
{ | ||
var json = JsonSerializer.Serialize(value, s_options); | ||
|
||
await SetJsonAsync(key, json).ConfigureAwait(false); | ||
} | ||
|
||
private ValueTask SetJsonAsync(string key, string json) | ||
=> _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, json); | ||
|
||
private ValueTask<string?> GetJsonAsync(string key) | ||
=> _jsRuntime.InvokeAsync<string?>("localStorage.getItem", key); | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Aspire.Dashboard/Model/BrowserStorage/SessionBrowserStorage.cs
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.