添加 AddConfigurationStore AddOperationalStore
string migrationsAssembly = Assembly.GetEntryAssembly().GetName().Name;
builder.Services.AddIdentityServer(options =>
{
options.IssuerUri = "null";
options.Authentication.CookieLifetime = TimeSpan.FromHours(2);
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
})
//添加这配置数据(客户端、资源)
.AddConfigurationStore(options => //添加配置数据(ConfigurationDbContext上下文用户配置数据)
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
})
//添加操作数据(codes、tokens、consents)
.AddOperationalStore(options => //添加操作数据(PersistedGrantDbContext上下文 临时数据(如授权和刷新令牌))
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
// 自动清理 token ,可选 //token自动清理间隔
options.EnableTokenCleanup = true;
// 自动清理 token ,可选 //token自动清理间隔
options.TokenCleanupInterval = 30;
token自动清理每次数量
//options.TokenCleanupBatchSize = 100;
})
//.AddInMemoryIdentityResources(Config.GetResources())
//.AddInMemoryApiScopes(Config.GetApiScopes())
//.AddInMemoryApiResources(Config.GetApis())
//.AddInMemoryClients(Config.GetClients(builder.Configuration))
.AddAspNetIdentity<ApplicationUser>()
.AddDeveloperSigningCredential(); // Not recommended for production - you need to store your key material somewhere secure
builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
修改配置 连接字符串 docker中的数据库连接字符串和项目中的不一至,docker文件中的数据库名称中Service后少了S 加上 和项目保持一至(有两处,开发和生产) 以保证生成用户信息和授权相关信息在一个数据库中
初始化 授权相关默认值
var configurationDbContext = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
EnsureSeedData(configurationDbContext, configuration,logger);
private static void EnsureSeedData(ConfigurationDbContext context, IConfiguration configuration, ILogger logger)
{
if (!context.Clients.Any())
{
logger.LogDebug("Clients 正在初始化");
foreach (var client in Config.GetClients(configuration))
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
if (!context.IdentityResources.Any())
{
logger.LogDebug("IdentityResources 正在初始化");
foreach (var resource in Config.GetResources())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
if (!context.ApiResources.Any())
{
logger.LogDebug("ApiResources 正在初始化");
foreach (var resource in Config.GetApis())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
if (!context.ApiScopes.Any())
{
logger.LogDebug("ApiScopes 正在初始化");
foreach (var resource in Config.GetApiScopes())
{
context.ApiScopes.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
add-migration InitialPersistedGrantDb -c PersistedGrantDbContext
Update-Database -Context PersistedGrantDbContext
add-migration InitialPersistedGrantDb -c ConfigurationDbContext
Update-Database -Context ConfigurationDbContext
如果执行失败,
1、指定Identity.API为启动项目
2、Program文件中migrationsAssembly指定为Identity.API
重新生成项目并启动 docker-compose正常登录