Skip to content

Instantly share code, notes, and snippets.

@ossentoo
Created July 13, 2020 21:06
Show Gist options
  • Save ossentoo/f09509d0d7006de10e5c844e8e6951b9 to your computer and use it in GitHub Desktop.
Save ossentoo/f09509d0d7006de10e5c844e8e6951b9 to your computer and use it in GitHub Desktop.

Revisions

  1. ossentoo revised this gist Jul 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Program.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    public class Program
    {
    const string WebAdminServerApi = "xperters.web.admin.ServerAPI";
    const string WebAdminServerApi = "application.web.admin.ServerAPI";

    public static async Task Main(string[] args)
    {
  2. ossentoo created this gist Jul 13, 2020.
    49 changes: 49 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    public class Program
    {
    const string WebAdminServerApi = "xperters.web.admin.ServerAPI";

    public static async Task Main(string[] args)
    {
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");

    Configure(builder.Services, builder.Configuration);

    await builder.Build().RunAsync();
    }

    public static void Configure(IServiceCollection services, IConfiguration configuration)
    {
    var apiBaseUri = configuration[Constants.ConfigKeyApiBaseUri];
    services.AddAuthorizationCore();
    services.AddBlazoredLocalStorage();

    services.AddScoped<ApiAuthenticationStateProvider>();
    services.AddScoped<AuthenticationStateProvider>(provider => provider.GetRequiredService<ApiAuthenticationStateProvider>());

    services.AddHttpClient(WebAdminServerApi, client => client.BaseAddress = new Uri(apiBaseUri))
    .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

    // Supply HttpClient instances that include access tokens when making requests to the server project
    services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>()
    .CreateClient(WebAdminServerApi));

    BindConfiguration(services, configuration);
    }

    private static void BindConfiguration(IServiceCollection services, IConfiguration configuration)
    {
    var accessToken = configuration[Constants.ConfigKeyAzureAdAccessTokenScope];

    var authenticationOptions = new MsalAuthenticationOptions();
    configuration.Bind(Constants.ConfigKeyAzureAd, authenticationOptions);

    services.AddMsalAuthentication(options =>
    {
    options.ProviderOptions.Authentication = authenticationOptions;
    options.ProviderOptions.DefaultAccessTokenScopes.Add(accessToken);
    });

    var tuple = new Tuple<string, MsalAuthenticationOptions>(accessToken, authenticationOptions);
    }
    }