From d18a033b1ee6d923a72d440718c5d496b57c2ffc Mon Sep 17 00:00:00 2001
From: Steve Sanderson
Date: Fri, 24 May 2019 15:28:37 +0100
Subject: [PATCH] Integrate AuthorizeView with actual authorization (#10487)
---
.../src/Hosting/WebAssemblyHostBuilder.cs | 7 +
.../src/Services/WebAssemblyConsoleLogger.cs | 34 +++
.../src/Services/WebAssemblyLoggerFactory.cs | 23 ++
.../Microsoft.AspNetCore.Components.csproj | 1 +
...etCore.Components.netstandard2.0.Manual.cs | 8 +-
.../src/Auth/AuthorizeDataAdapter.cs | 39 +++
.../Components/src/Auth/AuthorizeView.razor | 49 +++-
.../Microsoft.AspNetCore.Components.csproj | 1 +
.../Components/test/Auth/AuthorizeViewTest.cs | 275 ++++++++++++++++--
src/Components/Shared/test/TestRenderer.cs | 2 +-
src/Components/test/E2ETest/Tests/AuthTest.cs | 52 +++-
.../AuthTest/AuthorizeViewCases.razor | 34 ++-
.../ClientSideAuthenticationStateData.cs | 2 +-
.../ServerAuthenticationStateProvider.cs | 2 +-
.../test/testassets/BasicTestApp/Startup.cs | 6 +
.../TestServer/Controllers/UserController.cs | 20 +-
.../TestServer/Pages/Authentication.cshtml | 19 +-
.../test/testassets/TestServer/Startup.cs | 6 +
18 files changed, 529 insertions(+), 51 deletions(-)
create mode 100644 src/Components/Blazor/Blazor/src/Services/WebAssemblyConsoleLogger.cs
create mode 100644 src/Components/Blazor/Blazor/src/Services/WebAssemblyLoggerFactory.cs
create mode 100644 src/Components/Components/src/Auth/AuthorizeDataAdapter.cs
diff --git a/src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHostBuilder.cs b/src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHostBuilder.cs
index 8cf720e1ca..0b741cbd22 100644
--- a/src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHostBuilder.cs
+++ b/src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHostBuilder.cs
@@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Blazor.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
namespace Microsoft.AspNetCore.Blazor.Hosting
@@ -92,6 +94,7 @@ namespace Microsoft.AspNetCore.Blazor.Hosting
services.AddSingleton();
services.AddSingleton(WebAssemblyUriHelper.Instance);
services.AddSingleton(WebAssemblyNavigationInterception.Instance);
+ services.AddSingleton();
services.AddSingleton(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
@@ -102,6 +105,10 @@ namespace Microsoft.AspNetCore.Blazor.Hosting
};
});
+ // Needed for authorization
+ services.AddOptions();
+ services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(WebAssemblyConsoleLogger<>)));
+
foreach (var configureServicesAction in _configureServicesActions)
{
configureServicesAction(_BrowserHostBuilderContext, services);
diff --git a/src/Components/Blazor/Blazor/src/Services/WebAssemblyConsoleLogger.cs b/src/Components/Blazor/Blazor/src/Services/WebAssemblyConsoleLogger.cs
new file mode 100644
index 0000000000..c86c1cf30b
--- /dev/null
+++ b/src/Components/Blazor/Blazor/src/Services/WebAssemblyConsoleLogger.cs
@@ -0,0 +1,34 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using Microsoft.Extensions.Logging;
+
+namespace Microsoft.AspNetCore.Blazor.Services
+{
+ internal class WebAssemblyConsoleLogger : ILogger, ILogger
+ {
+ public IDisposable BeginScope(TState state)
+ {
+ return NoOpDisposable.Instance;
+ }
+
+ public bool IsEnabled(LogLevel logLevel)
+ {
+ return logLevel >= LogLevel.Warning;
+ }
+
+ public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)
+ {
+ var formattedMessage = formatter(state, exception);
+ Console.WriteLine($"[{logLevel}] {formattedMessage}");
+ }
+
+ private class NoOpDisposable : IDisposable
+ {
+ public static NoOpDisposable Instance = new NoOpDisposable();
+
+ public void Dispose() { }
+ }
+ }
+}
diff --git a/src/Components/Blazor/Blazor/src/Services/WebAssemblyLoggerFactory.cs b/src/Components/Blazor/Blazor/src/Services/WebAssemblyLoggerFactory.cs
new file mode 100644
index 0000000000..73458387e7
--- /dev/null
+++ b/src/Components/Blazor/Blazor/src/Services/WebAssemblyLoggerFactory.cs
@@ -0,0 +1,23 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using Microsoft.Extensions.Logging;
+
+namespace Microsoft.AspNetCore.Blazor.Services
+{
+ internal class WebAssemblyLoggerFactory : ILoggerFactory
+ {
+ public void AddProvider(ILoggerProvider provider)
+ {
+ // No-op
+ }
+
+ public ILogger CreateLogger(string categoryName)
+ => new WebAssemblyConsoleLogger
+
+ Roles:
+
+
@@ -37,7 +41,11 @@
Authenticated: @User.Identity.IsAuthenticated
Username: @User.Identity.Name
-
+ Roles:
+
+ @string.Join(", ", User.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToArray())
+
+ foreach
Sign out