Program.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Net.Http;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using System.Text;
  6. #if (!NoAuth && Hosted)
  7. using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
  8. #endif
  9. using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
  10. using Microsoft.Extensions.DependencyInjection;
  11. #if (Hosted)
  12. namespace ComponentsWebAssembly_CSharp.Client
  13. #else
  14. namespace ComponentsWebAssembly_CSharp
  15. #endif
  16. {
  17. public class Program
  18. {
  19. public static async Task Main(string[] args)
  20. {
  21. var builder = WebAssemblyHostBuilder.CreateDefault(args);
  22. builder.RootComponents.Add<App>("app");
  23. #if (!Hosted || NoAuth)
  24. builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
  25. #else
  26. builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
  27. .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
  28. // Supply HttpClient instances that include access tokens when making requests to the server project
  29. builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
  30. #endif
  31. #if(!NoAuth)
  32. #endif
  33. #if (IndividualLocalAuth)
  34. #if (Hosted)
  35. builder.Services.AddApiAuthorization();
  36. #else
  37. builder.Services.AddOidcAuthentication(options =>
  38. {
  39. #if(MissingAuthority)
  40. // Configure your authentication provider options here.
  41. // For more information, see https://aka.ms/blazor-standalone-auth
  42. #endif
  43. options.ProviderOptions.Authority = "https://login.microsoftonline.com/";
  44. options.ProviderOptions.ClientId = "33333333-3333-3333-33333333333333333";
  45. });
  46. #endif
  47. #endif
  48. #if (IndividualB2CAuth)
  49. builder.Services.AddMsalAuthentication(options =>
  50. {
  51. var authentication = options.ProviderOptions.Authentication;
  52. authentication.Authority = "https:////aadB2CInstance.b2clogin.com/qualified.domain.name/MySignUpSignInPolicyId";
  53. authentication.ClientId = "33333333-3333-3333-33333333333333333";
  54. authentication.ValidateAuthority = false;
  55. #if (Hosted)
  56. options.ProviderOptions.DefaultAccessTokenScopes.Add("https://qualified.domain.name/api.id.uri/api-scope");
  57. #endif
  58. });
  59. #endif
  60. #if(OrganizationalAuth)
  61. builder.Services.AddMsalAuthentication(options =>
  62. {
  63. var authentication = options.ProviderOptions.Authentication;
  64. authentication.Authority = "https://login.microsoftonline.com/22222222-2222-2222-2222-222222222222";
  65. authentication.ClientId = "33333333-3333-3333-33333333333333333";
  66. #if (Hosted)
  67. options.ProviderOptions.DefaultAccessTokenScopes.Add("api://api.id.uri/api-scope");
  68. #endif
  69. });
  70. #endif
  71. await builder.Build().RunAsync();
  72. }
  73. }
  74. }