Пример #1
0
        public Task StoreAsync(PersistedGrant token)
        {
            var existing = _context.PersistedGrants.SingleOrDefault(x => x.Key == token.Key);

            if (existing == null)
            {
                _logger.LogDebug("{persistedGrantKey} not found in database", token.Key);

                var persistedGrant = token.ToEntity();
                _context.PersistedGrants.Add(persistedGrant);
            }
            else
            {
                _logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }

            return(Task.FromResult(0));
        }
        public Task StoreAsync(PersistedGrant token)
        {
            var existing = _context.PersistedGrants.SingleOrDefault(x => x.SiteId == _siteId && x.Key == token.Key);

            if (existing == null)
            {
                var persistedGrant = token.ToEntity();
                persistedGrant.SiteId = _siteId;
                _context.PersistedGrants.Add(persistedGrant);
            }
            else
            {
                token.UpdateEntity(existing);
            }

            try
            {
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "StoreAsync");
            }

            return(Task.FromResult(0));
        }
Пример #3
0
        public Task StoreAsync(PersistedGrant token)
        {
            var existing = _context.PersistedGrants.SingleOrDefault(x => x.Key == token.Key);

            if (existing == null)
            {
                _logger.LogDebug("{persistedGrantKey} not found in database", token.Key);

                var persistedGrant = token.ToEntity();
                _context.PersistedGrants.Add(persistedGrant);
            }
            else
            {
                _logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
            }

            try
            {
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "Exception storing persisted grant");
            }

            return(Task.FromResult(0));
        }
Пример #4
0
        /// <summary>
        /// Stores the device authorization request.
        /// </summary>
        /// <param name="deviceCode">The device code.</param>
        /// <param name="userCode">The user code.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public virtual Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, DeviceCode data)
        {
            Context.DeviceFlowCodes.Add(ToEntity(data, deviceCode, userCode));

            Context.SaveChanges();

            return(Task.FromResult(0));
        }
Пример #5
0
        public void ClearTokens()
        {
            this._logger.LogTrace("Querying for tokens to clear");

            using (IServiceScope serviceScope = this._serviceProvider
                                                .GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (IPersistedGrantDbContext context = serviceScope
                                                          .ServiceProvider.GetService <IPersistedGrantDbContext>())
                {
                    PersistedGrant[] expired = context.PersistedGrants
                                               .Where(x => x.Expiration < DateTimeOffset.UtcNow)
                                               .ToArray();

                    this._logger.LogInformation(
                        "Clearing {tokenCount} tokens",
                        expired.Length);

                    if (expired.Length > 0)
                    {
                        context.PersistedGrants.RemoveRange(expired);
                        context.SaveChanges();
                    }
                }
            }
        }
Пример #6
0
        private Task RemoveDeviceCodes(IPersistedGrantDbContext context)
        {
            var found = Int32.MaxValue;

            while (found >= _options.TokenCleanupBatchSize)
            {
                var expiredCodes = context.DeviceFlowCodes
                                   .Where(x => x.Expiration < DateTime.UtcNow)
                                   .Take(_options.TokenCleanupBatchSize)
                                   .ToArray();

                found = expiredCodes.Length;
                _logger.LogInformation("Removing {deviceCodeCount} device flow codes", found);

                if (found > 0)
                {
                    context.DeviceFlowCodes.RemoveRange(expiredCodes);
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        // we get this if/when someone else already deleted the records
                        // we want to essentially ignore this, and keep working
                        _logger.LogDebug("Concurrency exception removing expired grants: {exception}", ex.Message);
                    }
                }
            }

            return(Task.CompletedTask);
        }
Пример #7
0
 public void ClearTokens()
 {
     try
     {
         _logger.LogTrace("Querying for tokens to clear", Array.Empty <object>());
         using (IServiceScope scope = _serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
         {
             using (IPersistedGrantDbContext service = scope.ServiceProvider.GetService <IPersistedGrantDbContext>())
             {
                 PersistedGrant[] array = service.PersistedGrants.Where(x => x.Expiration < (DateTimeOffset?)DateTimeOffset.UtcNow).ToArray();
                 _logger.LogInformation("Clearing {tokenCount} tokens", (object)array.Length);
                 if (array.Length == 0)
                 {
                     return;
                 }
                 service.PersistedGrants.RemoveRange(array);
                 service.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Exception clearing tokens: {exception}", (object)ex.Message);
     }
 }
Пример #8
0
        public Task StoreAsync(PersistedGrant token)
        {
            var persistedGrant = token.ToEntity();

            _context.PersistedGrants.Add(persistedGrant);
            try
            {
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "StoreAsync");
            }

            return(Task.FromResult(0));
        }
Пример #9
0
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveGrantsAsync()
        {
            var found = Int32.MaxValue;

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

                found = expiredGrants.Length;
                _logger.LogInformation("Removing {grantCount} grants", found);

                if (found > 0)
                {
                    _persistedGrantDbContext.PersistedGrants.RemoveRange(expiredGrants);
                    try
                    {
                        _persistedGrantDbContext.SaveChanges();

                        if (_operationalStoreNotification != null)
                        {
                            await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                        }
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        // we get this if/when someone else already deleted the records
                        // we want to essentially ignore this, and keep working
                        _logger.LogDebug("Concurrency exception removing expired grants: {exception}", ex.Message);
                    }
                }
            }
        }