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.
public class Program
{
const string WebAdminServerApi = "application.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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment