微服务eShopOnContainers实践--Identity.API sqlserver 持久化

添加 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正常登录

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值