public async Task SeedAsync(IdentityServerDbContext identityServerDbContext, ILogger <IdentityServerDbContextSeed> logger, IConfiguration configuration)
        {
            if (identityServerDbContext.Clients.Any())
            {
                return;
            }

            var retries = 3;
            var policy  = Policy.Handle <SqlException>()
                          .WaitAndRetryAsync(
                retryCount: retries,
                sleepDurationProvider: retry => TimeSpan.FromSeconds(5),
                onRetry: (ex, timeSpan, retry, ctx) =>
            {
                logger.LogWarning(ex, "Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", ex.GetType().Name, retry, retries);
            });


            var clients = Config.GetClients(configuration).Select(a => new IdentityServerClient()
            {
                ClientId   = a.ClientId,
                ClientName = a.ClientName,
                AllowAccessTokensViaBrowser      = a.AllowAccessTokensViaBrowser,
                AlwaysIncludeUserClaimsInIdToken = a.AlwaysIncludeUserClaimsInIdToken,
                RequireConsent         = a.RequireConsent,
                PostLogoutRedirectUris = a.PostLogoutRedirectUris.Select(plru => new ClientPostLogoutRedirectUri()
                {
                    PostLogoutRedirectUri = plru
                }).ToList(),
                RedirectUris = a.RedirectUris.Select(ru => new ClientRedirectUri()
                {
                    RedirectUri = ru
                }).ToList(),
                AllowedGrantTypes = a.AllowedGrantTypes.Select(agt => new ClientGrantType()
                {
                    GrantType = agt
                }).ToList(),
                AllowedScopes = a.AllowedScopes.Select(asc => new ClientScope()
                {
                    Scope = asc
                }).ToList(),
                ClientSecrets = a.ClientSecrets.Select(cs => new ClientSecret()
                {
                    Value = cs.Value
                }).ToList()
            });

            await policy.ExecuteAsync(async() =>
            {
                await identityServerDbContext.Clients.AddRangeAsync(clients);
                await identityServerDbContext.SaveChangesAsync();
            });
        }
Beispiel #2
0
 public EFClientStore(IdentityServerDbContext identityServerDbContext)
 {
     _identityServerDbContext = identityServerDbContext;
 }