From 638f54e7acd6fcd348444de80cb496b12e7c0ea3 Mon Sep 17 00:00:00 2001 From: Erland A Syafiq Date: Wed, 18 Oct 2023 15:22:58 -0400 Subject: [PATCH] Disallowed creation of the admin account when not enabled --- Program.cs | 33 ++++++++++++++++++++------------- README.md | 1 + 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Program.cs b/Program.cs index 4bdac58..7ba1430 100644 --- a/Program.cs +++ b/Program.cs @@ -65,29 +65,36 @@ public static async Task Main(string[] args) } } - using (var scope = app.Services.CreateScope()) + // Decides to create admin based on preferences + if (builder.Configuration["CREATE_ADMIN"] != "false") { - var userManager = scope.ServiceProvider.GetRequiredService>(); + System.Diagnostics.Debug.WriteLine("Admin Created"); + using (var scope = app.Services.CreateScope()) + { + var userManager = scope.ServiceProvider.GetRequiredService>(); - string email = "chargedaffaires@vtmunc.org"; - string? password = builder.Configuration["ADMIN_PASSWORD"]; + string email = "chargedaffaires@vtmunc.org"; + string? password = builder.Configuration["ADMIN_PASSWORD"]; - if (await userManager.FindByEmailAsync(email) == null) - { - var user = new ApplicationUser(); - user.UserName = email; - user.Email = email; - user.EmailConfirmed = true; + if (await userManager.FindByEmailAsync(email) == null) + { + var user = new ApplicationUser(); + user.UserName = email; + user.Email = email; + user.EmailConfirmed = true; - await userManager.CreateAsync(user, password); + await userManager.CreateAsync(user, password); - await userManager.AddToRoleAsync(user, "Admin"); + await userManager.AddToRoleAsync(user, "Admin"); - } + } + } } + + app.Run(); } } diff --git a/README.md b/README.md index 8f679a4..e9b5a79 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Set the admin password using the Secrets Manager. To do this, run the following ```bash dotnet user-secrets init dotnet user-secrets set "ADMIN_PASSWORD" "" +dotnet user-secrets set "CREATE_ADMIN" "" dotnet user-secrets list ```