Program.Main.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.AspNetCore.Components.Web;
  2. #if (!NoAuth && Hosted)
  3. using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
  4. #endif
  5. using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
  6. #if (Hosted)
  7. using ComponentsWebAssembly_CSharp.Client;
  8. #else
  9. using ComponentsWebAssembly_CSharp;
  10. #endif
  11. namespace Company.WebApplication1;
  12. public class Program
  13. {
  14. public static async Task Main(string[] args)
  15. {
  16. var builder = WebAssemblyHostBuilder.CreateDefault(args);
  17. builder.RootComponents.Add<App>("#app");
  18. builder.RootComponents.Add<HeadOutlet>("head::after");
  19. #if (!Hosted || NoAuth)
  20. builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
  21. #else
  22. builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
  23. .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
  24. // Supply HttpClient instances that include access tokens when making requests to the server project
  25. builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
  26. #endif
  27. #if(!NoAuth)
  28. #endif
  29. #if (IndividualLocalAuth)
  30. #if (Hosted)
  31. builder.Services.AddApiAuthorization();
  32. #else
  33. builder.Services.AddOidcAuthentication(options =>
  34. {
  35. #if(MissingAuthority)
  36. // Configure your authentication provider options here.
  37. // For more information, see https://aka.ms/blazor-standalone-auth
  38. #endif
  39. builder.Configuration.Bind("Local", options.ProviderOptions);
  40. });
  41. #endif
  42. #endif
  43. #if (IndividualB2CAuth)
  44. builder.Services.AddMsalAuthentication(options =>
  45. {
  46. builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
  47. #if (Hosted)
  48. options.ProviderOptions.DefaultAccessTokenScopes.Add("https://qualified.domain.name/api.id.uri/api-scope");
  49. #endif
  50. });
  51. #endif
  52. #if(OrganizationalAuth)
  53. builder.Services.AddMsalAuthentication(options =>
  54. {
  55. builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
  56. #if (Hosted)
  57. options.ProviderOptions.DefaultAccessTokenScopes.Add("api://api.id.uri/api-scope");
  58. #endif
  59. });
  60. #endif
  61. await builder.Build().RunAsync();
  62. }
  63. }