Skip to content

Commit cf8e25c

Browse files
authored
feat(Authorization): redesign service for IAuthorizationPolicyProvider (#4640)
* refactor: 兼容 web app wasm 模式 * refactor: 移除 AddAuthorizationCore 方法 * chore: 移除 System.Text.Json 依赖 * chore: bump version 9.0.0-rc.2.11.10.2
1 parent 8556413 commit cf8e25c

File tree

7 files changed

+19
-26
lines changed

7 files changed

+19
-26
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.0-rc.2.11.10.1</Version>
4+
<Version>9.0.0-rc.2.11.10.2</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Block/Block.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.Components.Authorization;
77
using Microsoft.AspNetCore.Components.Rendering;
8+
using Microsoft.Extensions.DependencyInjection;
89

910
namespace BootstrapBlazor.Components;
1011

@@ -61,8 +62,8 @@ public class Block : BootstrapComponentBase
6162
[Parameter]
6263
public RenderFragment? NotAuthorized { get; set; }
6364

64-
[Inject]
65-
private AuthenticationStateProvider? AuthenticationStateProvider { get; set; }
65+
[Inject, NotNull]
66+
private IServiceProvider? ServiceProvider { get; set; }
6667

6768
private bool IsShow { get; set; }
6869

@@ -92,9 +93,10 @@ private async Task<bool> ProcessAuthorizeAsync()
9293
{
9394
bool isAuthenticated = false;
9495
AuthenticationState? state = null;
95-
if (AuthenticationStateProvider != null)
96+
var provider = ServiceProvider.GetService<AuthenticationStateProvider>();
97+
if (provider != null)
9698
{
97-
state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
99+
state = await provider.GetAuthenticationStateAsync();
98100
}
99101
if (state != null)
100102
{

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,13 @@ public partial class Layout : IHandlerException
327327
[CascadingParameter]
328328
private Task<AuthenticationState>? AuthenticationStateTask { get; set; }
329329

330+
[Inject, NotNull]
331+
private IServiceProvider? ServiceProvider { get; set; }
332+
330333
[Inject]
331334
[NotNull]
332335
private IStringLocalizer<Layout>? Localizer { get; set; }
333336

334-
[Inject]
335-
private IAuthorizationPolicyProvider? AuthorizationPolicyProvider { get; set; }
336-
337-
[Inject]
338-
private IAuthorizationService? AuthorizationService { get; set; }
339-
340337
private bool _init { get; set; }
341338

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

378375
// 检查当前 Url
379376
if (OnAuthorizing != null)

src/BootstrapBlazor/Components/Tab/BootstrapBlazorAuthorizeView.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ public class BootstrapBlazorAuthorizeView : ComponentBase
4444
[CascadingParameter]
4545
private Task<AuthenticationState>? AuthenticationState { get; set; }
4646

47-
[Inject]
48-
private IAuthorizationPolicyProvider? AuthorizationPolicyProvider { get; set; }
49-
50-
[Inject]
51-
private IAuthorizationService? AuthorizationService { get; set; }
52-
5347
[Inject]
5448
[NotNull]
5549
private NavigationManager? NavigationManager { get; set; }
5650

51+
[Inject, NotNull]
52+
private IServiceProvider? ServiceProvider { get; set; }
53+
5754
private bool Authorized { get; set; }
5855

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

6965
/// <summary>

src/BootstrapBlazor/Directory.Build.props

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET6Version)" />
4242
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
4343
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(NET8Version)" />
44-
<PackageReference Include="System.Text.Json" Version="$(NET8Version)" />
4544
</ItemGroup>
4645

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

5856
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
@@ -62,7 +60,6 @@
6260
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
6361
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
6462
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET8Version)" />
65-
<PackageReference Include="System.Text.Json" Version="$(NET8Version)" />
6663
</ItemGroup>
6764

6865
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
@@ -72,7 +69,6 @@
7269
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET9Version)" />
7370
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET9Version)" />
7471
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET9Version)" />
75-
<PackageReference Include="System.Text.Json" Version="$(NET9Version)" />
7672
</ItemGroup>
7773

7874
<ItemGroup>

src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv
2626
services.AddMemoryCache();
2727
services.AddHttpClient();
2828

29-
services.AddAuthorizationCore();
3029
services.AddJsonLocalization(localizationConfigure);
3130

3231
services.AddConfiguration();

src/BootstrapBlazor/Extensions/TypeExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Components.Authorization;
8+
using Microsoft.Extensions.DependencyInjection;
89
using System.Reflection;
910

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

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

18-
public static async Task<bool> IsAuthorizedAsync(this Type type, Task<AuthenticationState>? authenticateState, IAuthorizationPolicyProvider? authorizePolicy, IAuthorizationService? authorizeService, object? resource = null)
19+
public static async Task<bool> IsAuthorizedAsync(this Type type, IServiceProvider serviceProvider, Task<AuthenticationState>? authenticateState, object? resource = null)
1920
{
2021
var ret = true;
2122
var authorizeData = AttributeAuthorizeDataCache.GetAuthorizeDataForType(type);
2223
if (authorizeData != null)
2324
{
2425
EnsureNoAuthenticationSchemeSpecified();
2526

27+
var authorizePolicy = serviceProvider.GetService<IAuthorizationPolicyProvider>();
28+
var authorizeService = serviceProvider.GetService<IAuthorizationService>();
2629
if (authenticateState != null && authorizePolicy != null && authorizeService != null)
2730
{
2831
var currentAuthenticationState = await authenticateState;

0 commit comments

Comments
 (0)