Skip to content

Commit defb801

Browse files
committed
refactor: 兼容 web app wasm 模式
1 parent 8556413 commit defb801

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

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/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)