Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/ProtectedBrowserStorage/ProtectedBrowserStorage.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29021.104
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ProtectedBrowserStorage", "src\Microsoft.AspNetCore.ProtectedBrowserStorage.csproj", "{955F1865-B258-47F9-8654-A6A44E08246E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ProtectedBrowserStorage.Tests", "test\Microsoft.AspNetCore.ProtectedBrowserStorage.Tests.csproj", "{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CD150B12-A9B3-4D67-B4D8-5A239A828804}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6B4DB52E-7008-4ECE-9D41-F3F92E52E468}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{45EE528E-42FB-46FE-BC4D-6A999AAAB312}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtectedBrowserStorageSample", "sample\ProtectedBrowserStorageSample\ProtectedBrowserStorageSample.csproj", "{05F8F23A-DCE0-479E-B333-94BF3DDCD835}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|x64.ActiveCfg = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|x64.Build.0 = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|x86.ActiveCfg = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Debug|x86.Build.0 = Debug|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|Any CPU.Build.0 = Release|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|x64.ActiveCfg = Release|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|x64.Build.0 = Release|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|x86.ActiveCfg = Release|Any CPU
{955F1865-B258-47F9-8654-A6A44E08246E}.Release|x86.Build.0 = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|x64.ActiveCfg = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|x64.Build.0 = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|x86.ActiveCfg = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Debug|x86.Build.0 = Debug|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|Any CPU.Build.0 = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|x64.ActiveCfg = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|x64.Build.0 = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|x86.ActiveCfg = Release|Any CPU
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6}.Release|x86.Build.0 = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|x64.ActiveCfg = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|x64.Build.0 = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|x86.ActiveCfg = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Debug|x86.Build.0 = Debug|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|Any CPU.Build.0 = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|x64.ActiveCfg = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|x64.Build.0 = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|x86.ActiveCfg = Release|Any CPU
{05F8F23A-DCE0-479E-B333-94BF3DDCD835}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{955F1865-B258-47F9-8654-A6A44E08246E} = {CD150B12-A9B3-4D67-B4D8-5A239A828804}
{AB09A93F-28E2-4AA3-AFF1-53E71E6521E6} = {6B4DB52E-7008-4ECE-9D41-F3F92E52E468}
{05F8F23A-DCE0-479E-B333-94BF3DDCD835} = {45EE528E-42FB-46FE-BC4D-6A999AAAB312}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51AEC06F-42A0-4CC7-BB10-98FE1A94375A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Startup).Assembly">
<NotFoundContent>
<p>Sorry, there's nothing at this address.</p>
</NotFoundContent>
</Router>
</CascadingAuthenticationState>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@page "/local-storage"
@using Microsoft.AspNetCore.ProtectedBrowserStorage
@inject ProtectedLocalStorage ProtectedLocalStore

<h1>Local storage</h1>

<p>
This page demonstrates how data can be stored in the browser's <code>localStorage</code>
collection. This data is scoped to your browser. So, if you set a value on the
counter then reload, your data will be retained. If you open multiple browser tabs,
each one will overwrite the data for the others, as they are all sharing the same
storage slot. If you close and reopen the entire browser, your data will still be restored next time you come back here.
</p>

<p>
Tip: In many cases, it's safer to use <a href="">session storage</a> because with local
storage, if a user opens multiple tabs, you may have bugs or confusing user experience
because the tabs will overwrite each other's data. Which is the right choice depends on
whether tabs should be independent, and whether you need to store the data across browser
application restarts.
</p>

<fieldset>
@if (currentCount.HasValue)
{
<p>Current count: <strong>@currentCount</strong></p>
<button @onclick="@IncrementCount">Increment</button>
}
</fieldset>

@functions {
int? currentCount;

protected override async Task OnInitAsync()
{
currentCount = await ProtectedLocalStore.GetAsync<int>("localCount");
}

async Task IncrementCount()
{
currentCount++;
await ProtectedLocalStore.SetAsync("localCount", currentCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
@page "/prerendering-support"
@using Microsoft.AspNetCore.ProtectedBrowserStorage
@inject ProtectedLocalStorage ProtectedLocalStore
@inject IComponentContext ComponentContext

<h1>Prerendering support</h1>

<p>
If you need to support prerendering while using browser-based storage, things are
a little more complicated. It's impossible to access browser-based storage during
prerendering, because there is no browser connection at that stage. So you need to avoid
calling <code>GetAsync</code> during prerendering, and defer the loading operation until
the client has connected back to the server.
</p>

<p>
This component demonstrates how to work with prerendering. To test this out, change
the value of <code>EnablePrerendering</code> to <code>true</code> inside <code>Startup.cs</code>,
then restart your application. You <strong>must</strong> then navigate directly to
this page's URL, otherwise you'll get an error, because the other pages don't support
prerendering.
</p>

<p>
Tip: In most cases, if you application is heavily built around use of browser-based storage,
it's better not to enable prerendering, since it adds complexity and doesn't really offer
any value, since you can't render a useful UI until the data has been loaded.
</p>

<fieldset>
@if (currentCount.HasValue)
{
<p>Current count: <strong>@currentCount</strong></p>
<button @onclick="@IncrementCount">Increment</button>
}
else
{
<p>Loading...</p>
}
</fieldset>

@functions {
int? currentCount;
bool isWaitingForConnection;

protected override async Task OnInitAsync()
{
if (ComponentContext.IsConnected)
{
// Looks like we're not prerendering, so we can immediately load
// the data from browser storage
await LoadStateAsync();
}
else
{
// We are prerendering, so have to defer the load operation until later
isWaitingForConnection = true;
}
}

protected override async Task OnAfterRenderAsync()
{
// By this stage we know the client has connected back to the server, and
// browser services are available. So if we didn't load the data earlier,
// we should do so now, then trigger a new render.
if (isWaitingForConnection)
{
isWaitingForConnection = false;
await LoadStateAsync();
StateHasChanged();
}
}

async Task LoadStateAsync()
{
currentCount = await ProtectedLocalStore.GetAsync<int>("prerenderedCount");
}

async Task SaveStateAsync()
{
await ProtectedLocalStore.SetAsync("prerenderedCount", currentCount);
}

Task IncrementCount()
{
currentCount++;
return SaveStateAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@page "/"
@using Microsoft.AspNetCore.ProtectedBrowserStorage
@inject ProtectedSessionStorage ProtectedSessionStore

<h1>Session storage</h1>

<p>
This page demonstrates how data can be stored in the browser's <code>sessionStorage</code>
collection. This data is scoped to your current browser tab. So, if you set a value on the
counter then reload, your data will be retained. If you open multiple browser tabs, each
one will persist its own independent value for the count across reloads, even though the
code uses the same storage key for them all.
</p>

<fieldset>
@if (currentCount.HasValue)
{
<p>Current count: <strong>@currentCount</strong></p>
<button @onclick="@IncrementCount">Increment</button>
}
</fieldset>

@functions {
int? currentCount;

protected override async Task OnInitAsync()
{
currentCount = await ProtectedSessionStore.GetAsync<int>("sessionCount");
}

async Task IncrementCount()
{
currentCount++;
await ProtectedSessionStore.SetAsync("sessionCount", currentCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@page "/"
@namespace ProtectedBrowserStorageSample.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ProtectedBrowserStorageSample</title>
<base href="~/" />
</head>
<body>
<app>
@if (Startup.EnablePrerendering)
{
@(await Html.RenderComponentAsync<App>())
}
else
{
<text>Loading...</text>
}
</app>

<script src="_framework/blazor.server.js"></script>
<script src="_content/Microsoft.AspNetCore.ProtectedBrowserStorage/protectedBrowserStorage.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@layout MainLayout
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ProtectedBrowserStorageSample
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.ProtectedBrowserStorage.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@inherits LayoutComponentBase

<NavLink href="">Session storage</NavLink> |
<NavLink href="local-storage">Local storage</NavLink> |
<NavLink href="prerendering-support">Prerendering support</NavLink>

<hr />

@Body
Loading