Esempio n. 1
0
    /// <summary>
    /// Removes the expired persisted grants.
    /// </summary>
    /// <returns></returns>
    protected virtual async Task RemoveExpiredPersistedGrantsAsync(CancellationToken cancellationToken = default)
    {
        var found = Int32.MaxValue;

        while (found >= _options.TokenCleanupBatchSize)
        {
            var expiredGrants = await _persistedGrantDbContext.PersistedGrants
                                .Where(x => x.Expiration < DateTime.UtcNow)
                                .OrderBy(x => x.Expiration)
                                .Take(_options.TokenCleanupBatchSize)
                                .ToArrayAsync(cancellationToken);

            found = expiredGrants.Length;

            if (found > 0)
            {
                _logger.LogInformation("Removing {grantCount} expired grants", found);

                _persistedGrantDbContext.PersistedGrants.RemoveRange(expiredGrants);

                var list = await _persistedGrantDbContext.SaveChangesWithConcurrencyCheckAsync <Entities.PersistedGrant>(_logger, cancellationToken);

                expiredGrants = expiredGrants.Except(list).ToArray();

                if (_operationalStoreNotification != null)
                {
                    await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                }
            }
        }
    }
Esempio n. 2
0
    /// <inheritdoc/>
    public async Task <IReadOnlyCollection <ServerSideSession> > GetAndRemoveExpiredSessionsAsync(int count, CancellationToken cancellationToken = default)
    {
        cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

        var entities = await Context.ServerSideSessions
                       .Where(x => x.Expires < DateTime.UtcNow)
                       .OrderBy(x => x.Id)
                       .Take(count)
                       .ToArrayAsync(cancellationToken);

        if (entities.Length > 0)
        {
            Context.ServerSideSessions.RemoveRange(entities);

            var list = await Context.SaveChangesWithConcurrencyCheckAsync <Entities.ServerSideSession>(Logger, cancellationToken);

            entities = entities.Except(list).ToArray();

            Logger.LogDebug("Found and removed {serverSideSessionCount} expired server-side sessions", entities.Length);
        }

        var results = entities.Select(entity => new ServerSideSession
        {
            Key         = entity.Key,
            Scheme      = entity.Scheme,
            SubjectId   = entity.SubjectId,
            SessionId   = entity.SessionId,
            DisplayName = entity.DisplayName,
            Created     = entity.Created,
            Renewed     = entity.Renewed,
            Expires     = entity.Expires,
            Ticket      = entity.Data,
        }).ToArray();

        return(results);
    }