Skip to content

Commit

Permalink
feat(Authorization): redesign service for IAuthorizationPolicyProvider (
Browse files Browse the repository at this point in the history
#4640)

* refactor: 兼容 web app wasm 模式

* refactor: 移除 AddAuthorizationCore 方法

* chore: 移除 System.Text.Json 依赖

* chore: bump version 9.0.0-rc.2.11.10.2
  • Loading branch information
ArgoZhang authored Nov 10, 2024
1 parent 8556413 commit cf8e25c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.0-rc.2.11.10.1</Version>
<Version>9.0.0-rc.2.11.10.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions src/BootstrapBlazor/Components/Block/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.DependencyInjection;

namespace BootstrapBlazor.Components;

Expand Down Expand Up @@ -61,8 +62,8 @@ public class Block : BootstrapComponentBase
[Parameter]
public RenderFragment? NotAuthorized { get; set; }

[Inject]
private AuthenticationStateProvider? AuthenticationStateProvider { get; set; }
[Inject, NotNull]
private IServiceProvider? ServiceProvider { get; set; }

private bool IsShow { get; set; }

Expand Down Expand Up @@ -92,9 +93,10 @@ private async Task<bool> ProcessAuthorizeAsync()
{
bool isAuthenticated = false;
AuthenticationState? state = null;
if (AuthenticationStateProvider != null)
var provider = ServiceProvider.GetService<AuthenticationStateProvider>();
if (provider != null)
{
state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
state = await provider.GetAuthenticationStateAsync();
}
if (state != null)
{
Expand Down
11 changes: 4 additions & 7 deletions src/BootstrapBlazor/Components/Layout/Layout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,13 @@ public partial class Layout : IHandlerException
[CascadingParameter]
private Task<AuthenticationState>? AuthenticationStateTask { get; set; }

[Inject, NotNull]
private IServiceProvider? ServiceProvider { get; set; }

[Inject]
[NotNull]
private IStringLocalizer<Layout>? Localizer { get; set; }

[Inject]
private IAuthorizationPolicyProvider? AuthorizationPolicyProvider { get; set; }

[Inject]
private IAuthorizationService? AuthorizationService { get; set; }

private bool _init { get; set; }

/// <summary>
Expand Down Expand Up @@ -373,7 +370,7 @@ protected override async Task OnInitializedAsync()
var context = RouteTableFactory.Create(AdditionalAssemblies, url);
if (context.Handler != null)
{
IsAuthenticated = await context.Handler.IsAuthorizedAsync(AuthenticationStateTask, AuthorizationPolicyProvider, AuthorizationService, Resource);
IsAuthenticated = await context.Handler.IsAuthorizedAsync(ServiceProvider, AuthenticationStateTask, Resource);

// 检查当前 Url
if (OnAuthorizing != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,13 @@ public class BootstrapBlazorAuthorizeView : ComponentBase
[CascadingParameter]
private Task<AuthenticationState>? AuthenticationState { get; set; }

[Inject]
private IAuthorizationPolicyProvider? AuthorizationPolicyProvider { get; set; }

[Inject]
private IAuthorizationService? AuthorizationService { get; set; }

[Inject]
[NotNull]
private NavigationManager? NavigationManager { get; set; }

[Inject, NotNull]
private IServiceProvider? ServiceProvider { get; set; }

private bool Authorized { get; set; }

/// <summary>
Expand All @@ -62,8 +59,7 @@ public class BootstrapBlazorAuthorizeView : ComponentBase
/// <returns></returns>
protected override async Task OnInitializedAsync()
{
Authorized = Type == null
|| await Type.IsAuthorizedAsync(AuthenticationState, AuthorizationPolicyProvider, AuthorizationService, Resource);
Authorized = Type == null || await Type.IsAuthorizedAsync(ServiceProvider, AuthenticationState, Resource);
}

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/BootstrapBlazor/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET6Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(NET8Version)" />
<PackageReference Include="System.Text.Json" Version="$(NET8Version)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
Expand All @@ -52,7 +51,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(NET8Version)" />
<PackageReference Include="System.Text.Json" Version="$(NET8Version)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand All @@ -62,7 +60,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET8Version)" />
<PackageReference Include="System.Text.Json" Version="$(NET8Version)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
Expand All @@ -72,7 +69,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET9Version)" />
<PackageReference Include="System.Text.Json" Version="$(NET9Version)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv
services.AddMemoryCache();
services.AddHttpClient();

services.AddAuthorizationCore();
services.AddJsonLocalization(localizationConfigure);

services.AddConfiguration();
Expand Down
5 changes: 4 additions & 1 deletion src/BootstrapBlazor/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace BootstrapBlazor.Components;
Expand All @@ -15,14 +16,16 @@ internal static class TypeExtensions

public static FieldInfo? GetFieldByName(this Type type, string fieldName) => CacheManager.GetRuntimeFields(type).Find(p => p.Name == fieldName);

public static async Task<bool> IsAuthorizedAsync(this Type type, Task<AuthenticationState>? authenticateState, IAuthorizationPolicyProvider? authorizePolicy, IAuthorizationService? authorizeService, object? resource = null)
public static async Task<bool> IsAuthorizedAsync(this Type type, IServiceProvider serviceProvider, Task<AuthenticationState>? authenticateState, object? resource = null)
{
var ret = true;
var authorizeData = AttributeAuthorizeDataCache.GetAuthorizeDataForType(type);
if (authorizeData != null)
{
EnsureNoAuthenticationSchemeSpecified();

var authorizePolicy = serviceProvider.GetService<IAuthorizationPolicyProvider>();
var authorizeService = serviceProvider.GetService<IAuthorizationService>();
if (authenticateState != null && authorizePolicy != null && authorizeService != null)
{
var currentAuthenticationState = await authenticateState;
Expand Down

0 comments on commit cf8e25c

Please sign in to comment.