Skip to content

[Blazor] Update AuthenticationStateProvider to use declarative persistent component state #62063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,20 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;

internal sealed class DeserializedAuthenticationStateProvider : AuthenticationStateProvider
{
// Do not change. This must match all versions of the server-side AuthenticationStateSerializer.PersistenceKey.
private const string PersistenceKey = $"__internal__{nameof(AuthenticationState)}";

private static readonly Task<AuthenticationState> _defaultUnauthenticatedTask =
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));

private readonly Task<AuthenticationState> _authenticationStateTask = _defaultUnauthenticatedTask;
private readonly Task<AuthenticationState> _authenticationStateTask;

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = $"{nameof(DeserializedAuthenticationStateProvider)} uses the {nameof(DynamicDependencyAttribute)} to preserve the necessary members.")]
[DynamicDependency(JsonSerialized, typeof(AuthenticationStateData))]
[DynamicDependency(JsonSerialized, typeof(IList<ClaimData>))]
[DynamicDependency(JsonSerialized, typeof(ClaimData))]
public DeserializedAuthenticationStateProvider(PersistentComponentState state, IOptions<AuthenticationStateDeserializationOptions> options)
{
if (!state.TryTakeFromJson<AuthenticationStateData?>(PersistenceKey, out var authenticationStateData) || authenticationStateData is null)
{
return;
}
[SupplyParameterFromPersistentComponentState]
private AuthenticationStateData? AuthStateData { get; set; }

_authenticationStateTask = options.Value.DeserializationCallback(authenticationStateData);

public DeserializedAuthenticationStateProvider(IOptions<AuthenticationStateDeserializationOptions> options)
{
_authenticationStateTask = AuthStateData is not null
? options.Value.DeserializationCallback(AuthStateData)
: _defaultUnauthenticatedTask;
}

public override Task<AuthenticationState> GetAuthenticationStateAsync() => _authenticationStateTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Infrastructure;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication.Internal;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -30,6 +31,7 @@ public static IServiceCollection AddAuthenticationStateDeserialization(this ISer
{
services.AddOptions();
services.TryAddScoped<AuthenticationStateProvider, DeserializedAuthenticationStateProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<DeserializedAuthenticationStateProvider>(services, RenderMode.InteractiveWebAssembly);
if (configure != null)
{
services.Configure(configure);
Expand Down
2 changes: 1 addition & 1 deletion src/JSInterop/Microsoft.JSInterop.JS/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
}
}
}
47 changes: 47 additions & 0 deletions src/JSInterop/Microsoft.JSInterop.JS/src/package.json.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@microsoft/dotnet-js-interop",
"version": "10.0.0-dev",
"description": "Provides abstractions and features for interop between .NET and JavaScript code.",
"main": "dist/src/Microsoft.JSInterop.js",
"types": "dist/src/Microsoft.JSInterop.d.ts",
"type": "module",
"scripts": {
"clean": "rimraf ./dist",
"test": "jest",
"test:watch": "jest --watch",
"test:debug": "node --nolazy --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --colors --verbose",
"build": "npm run clean && npm run build:esm",
"build:lint": "eslint -c .eslintrc.json --ext .ts ./src",
"build:esm": "tsc --project ./tsconfig.json",
"get-version": "node -e \"const { name, version } = require('./package.json'); console.log(`${name};${version}`);\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/dotnet/extensions.git"
},
"author": "Microsoft",
"license": "MIT",
"bugs": {
"url": "https://github.com/dotnet/aspnetcore/issues"
},
"homepage": "https://github.com/dotnet/aspnetcore/tree/main/src/JSInterop",
"files": [
"dist/**"
],
"devDependencies": {
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.23.6",
"@babel/preset-typescript": "^7.26.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"babel-jest": "^29.7.0",
"eslint": "^8.56.0",
"eslint-plugin-jsdoc": "^46.9.1",
"eslint-plugin-prefer-arrow": "^1.2.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-junit": "^16.0.0",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
}
}
6 changes: 3 additions & 3 deletions src/SignalR/clients/ts/signalr-protocol-msgpack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/signalr-protocol-msgpack",
"version": "5.0.0-dev",
"version": "10.0.0-dev",
"description": "MsgPack Protocol support for ASP.NET Core SignalR",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -41,11 +41,11 @@
"src/**/*"
],
"dependencies": {
"@microsoft/signalr": "*",
"@microsoft/signalr": ">=10.0.0-dev",
"@msgpack/msgpack": "^2.7.0"
},
"overrides": {
"ws": ">=7.4.6",
"tough-cookie": ">=4.1.3"
}
}
}
51 changes: 51 additions & 0 deletions src/SignalR/clients/ts/signalr-protocol-msgpack/package.json.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@microsoft/signalr-protocol-msgpack",
"version": "5.0.0-dev",
"description": "MsgPack Protocol support for ASP.NET Core SignalR",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"typings": "./dist/esm/index.d.ts",
"umd": "./dist/browser/signalr-protocol-msgpack.js",
"umd_name": "signalR.protocols.msgpack",
"unpkg": "./dist/browser/signalr-protocol-msgpack.js",
"directories": {
"test": "spec"
},
"sideEffects": false,
"scripts": {
"clean": "rimraf ./dist",
"prebuild": "rimraf ./src/pkg-version.ts && node -e \"const fs = require('fs'); const packageJson = require('./package.json'); fs.writeFileSync('./src/pkg-version.ts', 'export const VERSION = \\'' + packageJson.version + '\\';');\"",
"build": "npm run build:esm && npm run build:cjs && npm run build:browser && npm run build:uglify",
"build:esm": "tsc --project ./tsconfig.json --module es2015 --outDir ./dist/esm -d",
"build:cjs": "tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs",
"build:browser": "webpack-cli",
"build:uglify": "terser -m -c --ecma 2019 --module --source-map \"url='signalr-protocol-msgpack.min.js.map',content='./dist/browser/signalr-protocol-msgpack.js.map'\" --comments -o ./dist/browser/signalr-protocol-msgpack.min.js ./dist/browser/signalr-protocol-msgpack.js",
"get-version": "node -e \"const { name, version } = require('./package.json'); console.log(`${name};${version}`);\""
},
"keywords": [
"signalr",
"aspnetcore"
],
"repository": {
"type": "git",
"url": "git+https://github.com/dotnet/aspnetcore.git"
},
"author": "Microsoft",
"license": "MIT",
"bugs": {
"url": "https://github.com/dotnet/aspnetcore/issues"
},
"homepage": "https://github.com/dotnet/aspnetcore/tree/main/src/SignalR#readme",
"files": [
"dist/**/*",
"src/**/*"
],
"dependencies": {
"@microsoft/signalr": "*",
"@msgpack/msgpack": "^2.7.0"
},
"overrides": {
"ws": ">=7.4.6",
"tough-cookie": ">=4.1.3"
}
}
2 changes: 1 addition & 1 deletion src/SignalR/clients/ts/signalr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/signalr",
"version": "5.0.0-dev",
"version": "10.0.0-dev",
"description": "ASP.NET Core SignalR Client",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
54 changes: 54 additions & 0 deletions src/SignalR/clients/ts/signalr/package.json.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@microsoft/signalr",
"version": "5.0.0-dev",
"description": "ASP.NET Core SignalR Client",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"typings": "./dist/esm/index.d.ts",
"umd": "./dist/browser/signalr.js",
"umd_name": "signalR",
"unpkg": "./dist/browser/signalr.js",
"directories": {
"test": "spec"
},
"sideEffects": false,
"scripts": {
"clean": "rimraf ./dist",
"prebuild": "rimraf ./src/pkg-version.ts && node -e \"const fs = require('fs'); const packageJson = require('./package.json'); fs.writeFileSync('./src/pkg-version.ts', 'export const VERSION = \\'' + packageJson.version + '\\';');\"",
"build": "npm run build:esm && npm run build:cjs && npm run build:browser && npm run build:webworker",
"build:esm": "tsc --project ./tsconfig.json --module es2015 --outDir ./dist/esm -d",
"build:cjs": "tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs",
"build:browser": "webpack-cli",
"build:webworker": "webpack-cli --env platform=webworker",
"get-version": "node -e \"const { name, version } = require('./package.json'); console.log(`${name};${version}`);\""
},
"keywords": [
"signalr",
"aspnetcore"
],
"repository": {
"type": "git",
"url": "git+https://github.com/dotnet/aspnetcore.git"
},
"author": "Microsoft",
"license": "MIT",
"bugs": {
"url": "https://github.com/dotnet/aspnetcore/issues"
},
"homepage": "https://github.com/dotnet/aspnetcore/tree/main/src/SignalR#readme",
"files": [
"dist/**/*",
"src/**/*"
],
"dependencies": {
"abort-controller": "^3.0.0",
"eventsource": "^2.0.2",
"fetch-cookie": "^2.0.3",
"node-fetch": "^2.6.7",
"ws": "^7.5.10"
},
"overrides": {
"ansi-regex": "5.0.1",
"tough-cookie": ">=4.1.3"
}
}
Loading