281 字
1 分鐘
Project Initialization Process with IdentityServer4 + ASP.Net Identity
Conclusion
Initialize IdentityServer4 + user system in one go using the is4aspid template + Migration + Seed.
Where It’s Suitable
- Need to quickly set up an OAuth / OpenID Connect Server
- Want to integrate ASP.NET Identity (username/password login)
- Building an internal authentication service or SSO system
- Testing or learning IdentityServer architecture
Steps
1. Install Template and Create Project
- Use
dotnet newto install the IdentityServer4 template - Create a project integrating ASP.NET Identity (is4aspid)
- Why: This template comes with built-in login and user management, so you don’t have to implement it yourself.
# 安裝範本dotnet new -i identityserver4.templates
# 建立專案(含 ASP.NET Identity)dotnet new is4aspid -n IdentityServerAspNetIdentityCommon Templates Comparison:
| Template | Purpose |
|---|---|
| is4empty | Empty project |
| is4inmem | In-memory testing |
| is4ef | DB storage configuration |
| is4aspid | Integrate username/password login (recommended) |
2. Create Database (Migration)
- Add Migration (generate table schema)
- Update database (create Identity + IdentityServer Tables)
- Why: IdentityServer needs a DB to store client / user / config
# 建立 Migrationdotnet ef migrations add InitialCreate
# 建立資料表dotnet ef database update3. Initialize Seed Data
- Add
/seedparameter when starting the project - Automatically writes test users, Client settings
- Why: Avoids manual data creation, allowing direct testing of the login process
dotnet run /seedCore code:
var seed = args.Contains("/seed");
if (seed){ args = args.Except(new[] { "/seed" }).ToArray();}
var host = CreateHostBuilder(args).Build();
if (seed){ // 初始化資料 var config = host.Services.GetRequiredService<IConfiguration>(); var connectionString = config.GetConnectionString("DefaultConnection");
SeedData.EnsureSeedData(connectionString); return 0;}Additional Notes
- IdentityServer4 is no longer maintained; for production projects, it’s recommended to evaluate Duende (paid).
- Seed is only suitable for development environments; for production, use Migration + Script for management.
- The
/seeddesign is convenient, but avoid misusing it in Production.
Wrap-up
Three steps to complete authentication system initialization. The key is not writing code, but choosing the right template. In engineering: first make it run, then optimize.
Project Initialization Process with IdentityServer4 + ASP.Net Identity
https://joyceowo.github.io/posts/en/23ba78ea09fa81dc9fe8e63d47cb55ad/