281 字
1 分鐘
瀏覽次數
Project Initialization Process with IdentityServer4 + ASP.Net Identity
2025-07-25
2026-04-13

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 new to 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.
Terminal window
# 安裝範本
dotnet new -i identityserver4.templates
# 建立專案(含 ASP.NET Identity)
dotnet new is4aspid -n IdentityServerAspNetIdentity

Common Templates Comparison:

TemplatePurpose
is4emptyEmpty project
is4inmemIn-memory testing
is4efDB storage configuration
is4aspidIntegrate 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
Terminal window
# 建立 Migration
dotnet ef migrations add InitialCreate
# 建立資料表
dotnet ef database update

3. Initialize Seed Data#

  • Add /seed parameter when starting the project
  • Automatically writes test users, Client settings
  • Why: Avoids manual data creation, allowing direct testing of the login process
Terminal window
dotnet run /seed

Core 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 /seed design 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/
作者
JoyceOwO
發佈於
2025-07-25
許可協議
CC BY-NC-SA 4.0