On every startup in the Development environment,Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Andrespeerez/porfolio-blog/llms.txt
Use this file to discover all available pages before exploring further.
AdminUserSeeder.SeedAsync() checks whether the configured admin email already exists in the database and creates the user if it doesn’t. This means you never need to manually run SQL scripts, open a database browser, or call a registration endpoint just to get a working admin account — cloning the repository and running dotnet run is enough to have a fully authenticated local environment within seconds.
How seeding works
Seeder is invoked at application startup
Program.cs creates a DI scope immediately after the application is built and resolves AdminUserSeeder from the service container. SeedAsync() is called and awaited before the HTTP pipeline begins accepting requests, guaranteeing the admin account is available from the very first page load.Program.cs
Environment guard — exits immediately outside Development
The very first thing
SeedAsync() does is check the current hosting environment. If the application is not running under Development, the method returns without touching the database. This makes the seeder a true no-op in staging and production environments.AdminUserSeeder.cs
Reads seed credentials from configuration
The seeder reads
Seed:AdminEmail and Seed:AdminPassword from the IConfiguration abstraction. In practice these values come from appsettings.Development.json. If either value is missing or whitespace-only, the seeder logs nothing and exits — preventing a silent failure from creating an account with empty credentials.AdminUserSeeder.cs
Checks whether the admin user already exists
IUserRepository.GetByEmailAsync(email) queries the database for a user matching the configured email address. If a record is found, the seeder prints a message to the console and returns without modifying anything. This makes every startup idempotent — running the application ten times creates exactly one admin user.AdminUserSeeder.cs
Creates and persists the admin user
When no existing user is found,
User.Create(email, password, _passwordHasher) constructs a new User domain entity. The IPasswordHasher implementation (backed by ASP.NET Core Identity’s PasswordHasher<User>) hashes the plain-text password before it is stored. The new user is then persisted via IUserRepository.AddAsync(user).AdminUserSeeder.cs
Default seed credentials
The out-of-the-box credentials shipped inappsettings.Development.json are:
appsettings.Development.json
http://localhost:5058/login after your first dotnet run.
AdminUserSeeder source
The complete seeder implementation for reference:AdminUserSeeder.cs
IUserRepository for database access, IPasswordHasher for hashing the plain-text password, IConfiguration for reading the credentials, and IWebHostEnvironment for the environment guard.
Customising seed credentials
You can override the seed credentials at runtime using environment variables — no file modifications needed. ASP.NET Core’s configuration system automatically picks up environment variables that match the config key hierarchy.appsettings.json and appsettings.Development.json, so you can keep the default file untouched and supply real credentials at runtime in CI or shared dev environments.
Registration in Program.cs
AdminUserSeeder is registered as a transient service so the DI container always creates a fresh instance — appropriate for a one-shot startup task. The seeder is then resolved from a manually created scope and invoked before the HTTP middleware pipeline is started:
Program.cs
using block ensures the scope — and all scoped services it holds, such as AppDbContext — are disposed cleanly after seeding completes, regardless of whether SeedAsync() succeeds or throws.
The seeder runs before
app.UseAuthentication() and app.UseAuthorization() are called, so there is no risk of the middleware pipeline interfering with database writes during the seed operation.