|
1 |
| -using FSH.BlazorWebAssembly.Client.Infrastructure.Authentication; |
| 1 | +using System.Globalization; |
| 2 | +using System.Reflection; |
| 3 | +using FSH.BlazorWebAssembly.Client.Infrastructure.Authentication; |
2 | 4 | using FSH.BlazorWebAssembly.Client.Infrastructure.Managers;
|
3 | 5 | using FSH.BlazorWebAssembly.Client.Infrastructure.Managers.Preferences;
|
4 | 6 | using Microsoft.AspNetCore.Authorization;
|
5 | 7 | using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
6 | 8 | using Microsoft.Extensions.DependencyInjection;
|
7 |
| -using Microsoft.Extensions.Options; |
8 | 9 | using MudBlazor;
|
9 | 10 | using MudBlazor.Services;
|
10 |
| -using System.Globalization; |
11 |
| -using System.Reflection; |
12 | 11 | using Toolbelt.Blazor.Extensions.DependencyInjection;
|
13 | 12 |
|
14 |
| -namespace FSH.BlazorWebAssembly.Client.Infrastructure.Extensions |
| 13 | +namespace FSH.BlazorWebAssembly.Client.Infrastructure.Extensions; |
| 14 | + |
| 15 | +public static class WebAssemblyHostBuilderExtensions |
15 | 16 | {
|
16 |
| - public static class WebAssemblyHostBuilderExtensions |
| 17 | + private const string ClientName = "FullStackHero.API"; |
| 18 | + public static WebAssemblyHostBuilder AddClientServices(this WebAssemblyHostBuilder builder, WebAssemblyHostConfiguration configs) |
17 | 19 | {
|
18 |
| - private const string ClientName = "FullStackHero.API"; |
19 |
| - public static WebAssemblyHostBuilder AddClientServices(this WebAssemblyHostBuilder builder, WebAssemblyHostConfiguration configs) |
20 |
| - { |
21 |
| - builder |
22 |
| - .Services |
23 |
| - .AddDistributedMemoryCache() |
24 |
| - .AddLocalization(options => |
25 |
| - { |
26 |
| - options.ResourcesPath = "Resources"; |
27 |
| - }) |
28 |
| - .AddAuthorizationCore(options => |
29 |
| - { |
30 |
| - RegisterPermissionClaims(options); |
31 |
| - }) |
32 |
| - .AddBlazoredLocalStorage() |
33 |
| - .AddMudServices(configuration => |
34 |
| - { |
35 |
| - configuration.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomRight; |
36 |
| - configuration.SnackbarConfiguration.HideTransitionDuration = 100; |
37 |
| - configuration.SnackbarConfiguration.ShowTransitionDuration = 100; |
38 |
| - configuration.SnackbarConfiguration.VisibleStateDuration = 3000; |
39 |
| - configuration.SnackbarConfiguration.ShowCloseIcon = false; |
40 |
| - }) |
41 |
| - .AddScoped<ClientPreferenceManager>() |
42 |
| - .AddScoped<ApplicationAuthenticationStateProvider>() |
43 |
| - .AddScoped<AuthenticationStateProvider, ApplicationAuthenticationStateProvider>() |
44 |
| - .AddTransient<AuthenticationHeaderHandler>() |
45 |
| - .AutoRegisterInterfaces<IManager>() |
46 |
| - .AutoRegisterInterfaces<IApiService>() |
47 |
| - .AddScoped(sp => sp |
48 |
| - .GetRequiredService<IHttpClientFactory>() |
49 |
| - .CreateClient(ClientName).EnableIntercept(sp)) |
50 |
| - .AddHttpClient(ClientName, client => |
51 |
| - { |
52 |
| - client.DefaultRequestHeaders.AcceptLanguage.Clear(); |
53 |
| - client.DefaultRequestHeaders.AcceptLanguage.ParseAdd(CultureInfo.DefaultThreadCurrentCulture?.TwoLetterISOLanguageName); |
54 |
| - client.BaseAddress = new Uri(configs.GetValue<string>(ClientName)); |
55 |
| - }) |
56 |
| - .AddHttpMessageHandler<AuthenticationHeaderHandler>(); |
57 |
| - builder.Services.AddHttpClientInterceptor(); |
58 |
| - return builder; |
59 |
| - } |
| 20 | + builder |
| 21 | + .Services |
| 22 | + .AddDistributedMemoryCache() |
| 23 | + .AddLocalization(options => |
| 24 | + { |
| 25 | + options.ResourcesPath = "Resources"; |
| 26 | + }) |
| 27 | + .AddAuthorizationCore(options => |
| 28 | + { |
| 29 | + RegisterPermissionClaims(options); |
| 30 | + }) |
| 31 | + .AddBlazoredLocalStorage() |
| 32 | + .AddMudServices(configuration => |
| 33 | + { |
| 34 | + configuration.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomRight; |
| 35 | + configuration.SnackbarConfiguration.HideTransitionDuration = 100; |
| 36 | + configuration.SnackbarConfiguration.ShowTransitionDuration = 100; |
| 37 | + configuration.SnackbarConfiguration.VisibleStateDuration = 3000; |
| 38 | + configuration.SnackbarConfiguration.ShowCloseIcon = false; |
| 39 | + }) |
| 40 | + .AddScoped<ClientPreferenceManager>() |
| 41 | + .AddScoped<ApplicationAuthenticationStateProvider>() |
| 42 | + .AddScoped<AuthenticationStateProvider, ApplicationAuthenticationStateProvider>() |
| 43 | + .AddTransient<AuthenticationHeaderHandler>() |
| 44 | + .AutoRegisterInterfaces<IManager>() |
| 45 | + .AutoRegisterInterfaces<IApiService>() |
| 46 | + .AddScoped(sp => sp |
| 47 | + .GetRequiredService<IHttpClientFactory>() |
| 48 | + .CreateClient(ClientName).EnableIntercept(sp)) |
| 49 | + .AddHttpClient(ClientName, client => |
| 50 | + { |
| 51 | + client.DefaultRequestHeaders.AcceptLanguage.Clear(); |
| 52 | + client.DefaultRequestHeaders.AcceptLanguage.ParseAdd(CultureInfo.DefaultThreadCurrentCulture?.TwoLetterISOLanguageName); |
| 53 | + client.BaseAddress = new Uri(configs.GetValue<string>(ClientName)); |
| 54 | + }) |
| 55 | + .AddHttpMessageHandler<AuthenticationHeaderHandler>(); |
| 56 | + builder.Services.AddHttpClientInterceptor(); |
| 57 | + return builder; |
| 58 | + } |
60 | 59 |
|
61 |
| - public static IServiceCollection AutoRegisterInterfaces<T>(this IServiceCollection services) |
62 |
| - { |
63 |
| - var @interface = typeof(T); |
| 60 | + public static IServiceCollection AutoRegisterInterfaces<T>(this IServiceCollection services) |
| 61 | + { |
| 62 | + var @interface = typeof(T); |
64 | 63 |
|
65 |
| - var types = @interface |
66 |
| - .Assembly |
67 |
| - .GetExportedTypes() |
68 |
| - .Where(t => t.IsClass && !t.IsAbstract) |
69 |
| - .Select(t => new |
70 |
| - { |
71 |
| - Service = t.GetInterface($"I{t.Name}"), |
72 |
| - Implementation = t |
73 |
| - }) |
74 |
| - .Where(t => t.Service != null); |
| 64 | + var types = @interface |
| 65 | + .Assembly |
| 66 | + .GetExportedTypes() |
| 67 | + .Where(t => t.IsClass && !t.IsAbstract) |
| 68 | + .Select(t => new |
| 69 | + { |
| 70 | + Service = t.GetInterface($"I{t.Name}"), |
| 71 | + Implementation = t |
| 72 | + }) |
| 73 | + .Where(t => t.Service != null); |
75 | 74 |
|
76 |
| - foreach (var type in types) |
| 75 | + foreach (var type in types) |
| 76 | + { |
| 77 | + if (@interface.IsAssignableFrom(type.Service)) |
77 | 78 | {
|
78 |
| - if (@interface.IsAssignableFrom(type.Service)) |
79 |
| - { |
80 |
| - services.AddTransient(type.Service, type.Implementation); |
81 |
| - } |
| 79 | + services.AddTransient(type.Service, type.Implementation); |
82 | 80 | }
|
83 |
| - |
84 |
| - return services; |
85 | 81 | }
|
86 | 82 |
|
87 |
| - private static void RegisterPermissionClaims(AuthorizationOptions options) |
| 83 | + return services; |
| 84 | + } |
| 85 | + |
| 86 | + private static void RegisterPermissionClaims(AuthorizationOptions options) |
| 87 | + { |
| 88 | + foreach (var prop in typeof(PermissionConstants).GetNestedTypes().SelectMany(c => c.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))) |
88 | 89 | {
|
89 |
| - foreach (var prop in typeof(PermissionConstants).GetNestedTypes().SelectMany(c => c.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))) |
| 90 | + object propertyValue = prop.GetValue(null); |
| 91 | + if (propertyValue is not null) |
90 | 92 | {
|
91 |
| - object propertyValue = prop.GetValue(null); |
92 |
| - if (propertyValue is not null) |
93 |
| - { |
94 |
| - options.AddPolicy(propertyValue.ToString(), policy => policy.RequireClaim(ClaimConstants.Permission, propertyValue.ToString())); |
95 |
| - } |
| 93 | + options.AddPolicy(propertyValue.ToString(), policy => policy.RequireClaim(ClaimConstants.Permission, propertyValue.ToString())); |
96 | 94 | }
|
97 | 95 | }
|
98 | 96 | }
|
|
0 commit comments