第一步:创建 NetCore 项目
第二步:安装 Nuget 包
IdentityServer4
IdentityServer4.AspNetIdentity
IdentityServer4.EntityFramework
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
第三步:配置(appsettings.json)数据库连接字符串
"ConnectionStrings": {
"DefaultConnection": "Server=.;database=microids4db;uid=sa;pwd=.mssql.;TrustServerCertificate=True;"
}

第四步:配置数据库服务
//读取数据库连接
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
/**********************************IdentityServer4持久化配置**********************************/
//添加用户数据上下文 ApplicationDbContext
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
//添加配置数据上下文 ConfigurationDbContext、操作数据上下文 PersistedGrantDbContext、用户持久化
builder.Services.AddIdentityServer()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
{
builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly("Micro.IdentityServer4V4"));
};
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
{
builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly("Micro.IdentityServer4V4"));
};
//token配置
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddAspNetIdentity<IdentityUser>()
.AddDeveloperSigningCredential();

第五步:控制台迁移
1.
add-migration InitialPersisted -c PersistedGrantDbContext -o Migrations/Persisted
update-database -Context PersistedGrantDbContext