This commit is contained in:
2023-01-28 13:21:47 -06:00
parent 11b387b928
commit 8a25c0ed35
26 changed files with 443 additions and 359 deletions

View File

@@ -17,49 +17,55 @@ await builder.Build().RunAsync();
static void ConfigureDependencies(IServiceCollection services, IConfiguration configuration)
{
/**
/**
* Why two HTTP clients?
* See qhttps://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?source=recommendations&view=aspnetcore-6.0#unauthenticated-or-unauthorized-web-api-requests-in-an-app-with-a-secure-default-client
*/
var shogiApiUrl = new Uri(configuration["ShogiApiUrl"], UriKind.Absolute);
services
.AddHttpClient(ShogiApi.MsalClientName, client => client.BaseAddress = shogiApiUrl)
.AddHttpMessageHandler<MsalMessageHandler>();
services
.AddHttpClient(ShogiApi.GuestClientName, client => client.BaseAddress = shogiApiUrl)
.AddHttpMessageHandler<CookieCredentialsMessageHandler>();
var baseUrl = configuration["ShogiApiUrl"];
if (string.IsNullOrWhiteSpace(baseUrl))
{
throw new InvalidOperationException("ShogiApiUrl configuration is missing.");
}
// Authorization
services.AddMsalAuthentication(options =>
{
configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.LoginMode = "redirect";
});
services.AddOidcAuthentication(options =>
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
configuration.Bind("AzureAd", options.ProviderOptions);
options.ProviderOptions.ResponseType = "code";
});
var shogiApiUrl = new Uri(baseUrl, UriKind.Absolute);
services
.AddHttpClient(ShogiApi.MsalClientName, client => client.BaseAddress = shogiApiUrl)
.AddHttpMessageHandler<MsalMessageHandler>();
services
.AddHttpClient(ShogiApi.GuestClientName, client => client.BaseAddress = shogiApiUrl)
.AddHttpMessageHandler<CookieCredentialsMessageHandler>();
// https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-6.0#service-lifetime
services.AddScoped<ClientWebSocket>();
services.AddScoped<AccountManager>();
services.AddScoped<AccountState>();
services.AddScoped<ShogiSocket>();
services.AddScoped<ModalService>();
services.AddScoped<ILocalStorage, LocalStorage>();
services.AddScoped<MsalMessageHandler>();
services.AddScoped<CookieCredentialsMessageHandler>();
services.AddScoped<IShogiApi, ShogiApi>();
services.AddScoped<PromotePrompt>();
// Authorization
services.AddMsalAuthentication(options =>
{
configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.LoginMode = "redirect";
});
services.AddOidcAuthentication(options =>
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
configuration.Bind("AzureAd", options.ProviderOptions);
options.ProviderOptions.ResponseType = "code";
});
var serializerOptions = new JsonSerializerOptions
{
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
services.AddScoped((sp) => serializerOptions);
// https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-6.0#service-lifetime
services.AddScoped<ClientWebSocket>();
services.AddScoped<AccountManager>();
services.AddScoped<AccountState>();
services.AddScoped<ShogiSocket>();
services.AddScoped<ModalService>();
services.AddScoped<ILocalStorage, LocalStorage>();
services.AddScoped<MsalMessageHandler>();
services.AddScoped<CookieCredentialsMessageHandler>();
services.AddScoped<IShogiApi, ShogiApi>();
services.AddScoped<PromotePrompt>();
var serializerOptions = new JsonSerializerOptions
{
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
services.AddScoped((sp) => serializerOptions);
}