Skip to content

Commit

Permalink
Disallowed creation of the admin account when not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
erland-syafiq committed Oct 18, 2023
1 parent c0fc359 commit 638f54e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
33 changes: 20 additions & 13 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserManager<ApplicationUser>>();
System.Diagnostics.Debug.WriteLine("Admin Created");
using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();

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();
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" "<INSERT_PASSWORD>"
dotnet user-secrets set "CREATE_ADMIN" "<true or false>"
dotnet user-secrets list
```

Expand Down

0 comments on commit 638f54e

Please sign in to comment.