public async Task StoreAsync(PersistedGrant token)
        {
            var site = _contextAccessor.HttpContext.GetTenant<SiteContext>();
            if (site == null)
            {
                _logger.LogError("sitecontext was null");
                return;
            }
            var _siteId = site.Id.ToString();

            var existing = await _context.PersistedGrants.SingleOrDefaultAsync(x => x.SiteId == _siteId && x.Key == token.Key)
                .ConfigureAwait(false);
            if (existing == null)
            {
                var persistedGrant = token.ToEntity();
                persistedGrant.SiteId = _siteId;
                _context.PersistedGrants.Add(persistedGrant);
            }
            else
            {
                token.UpdateEntity(existing);
            }

            try
            {
                await _context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "StoreAsync");
            }  
        }
Example #2
0
        /// <inheritdoc/>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var snapshot = await Context.PersistedGrants
                           .WhereEqualTo("Key", token.Key)
                           .Limit(1)
                           .GetSnapshotAsync()
                           .ConfigureAwait(false);

            DocumentReference docRef;

            Entities.PersistedGrant entity;

            if (snapshot.Count == 0)
            {
                Logger.LogDebug("{persistedGrantKey} not found in database", token.Key);
                docRef = Context.PersistedGrants.Document();
                entity = token.ToEntity();
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);
                docRef = snapshot[0].Reference;
                entity = snapshot[0].ConvertTo <Entities.PersistedGrant>();
                token.UpdateEntity(entity);
            }

            await docRef.SetAsync(entity).ConfigureAwait(false);
        }
Example #3
0
        /// <inheritdoc />
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await Session.Query <Entities.PersistedGrant, PersistentGrantIndex>()
                           .SingleOrDefaultAsync(x => x.Key == token.Key);

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

                var persistedGrant = token.ToEntity();
                await Session.StoreAsync(persistedGrant);
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
            }

            try
            {
                await Session.WaitForIndexAndSaveChangesAsync <PersistentGrantIndex>();
            }
            catch (Exception ex)
            {
                Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }
        }
        /// <inheritdoc/>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await Context.PersistedGrants.AsQueryable().SingleOrDefaultAsync(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
            {
                await Context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }
        }
Example #5
0
        /// <inheritdoc/>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await FreeSql.Select <Entities.PersistedGrant>().Where(x => x.Key == token.Key).FirstAsync();

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

                    var persistedGrant = token.ToEntity();
                    await FreeSql.Insert(persistedGrant).ExecuteAffrowsAsync();
                }
                else
                {
                    Logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                    token.UpdateEntity(existing);
                    await FreeSql.Update <Entities.PersistedGrant>().SetSource(existing).ExecuteAffrowsAsync();
                }
            }
            catch (Exception ex)
            {
                Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }
        }
        public Task StoreAsync(PersistedGrant token)
        {
            var existing = _context.PersistedGrants.AsQueryable().SingleOrDefault(x => x.Key == token.Key);

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

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

                    token.UpdateEntity(existing);
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }

            return(Task.FromResult(0));
        }
Example #7
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));
        }
Example #8
0
        /// <summary>
        /// 新增授权信息
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await Context.Query <Entities.PersistedGrant>().FirstOrDefaultAsync(x => x.Key == token.Key);

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

                var persistedGrant = token.ToEntity();
                await Context.Command <Entities.PersistedGrant>().AddAsync(persistedGrant);
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);
                //更新实体传输
                token.UpdateEntity(existing);
                //执行命令
                await Context.Command <Entities.PersistedGrant>().UpdateAsync(x => x.Key == token.Key, new Dictionary <string, object>()
                {
                    { "Type", token.Type },
                    { "SubjectId", token.SubjectId },
                    { "ClientId", token.ClientId },
                    { "CreationTime", token.CreationTime },
                    { "Expiration", token.Expiration },
                    { "Data", token.Data }
                });
            }
        }
        public Task StoreAsync(PersistedGrant token)
        {
            try
            {
                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.Add(persistedGrant);
                }
                else
                {
                    _logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                    token.UpdateEntity(existing);
                    _context.Update(x => x.Key == token.Key, existing);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "Exception storing persisted grant");
            }

            return(Task.FromResult(0));
        }
        /// <inheritdoc />
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            using (var session = OpenAsyncSession())
            {
                var hashedTokenKey = CryptographyHelper.CreateHash(token.Key);

                var existing = await session.Query <Entities.PersistedGrant, PersistedGrantIndex>()
                               .SingleOrDefaultAsync(x => x.Key == hashedTokenKey);

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

                    var persistedGrant = token.ToEntity();
                    await session.StoreAsync(persistedGrant);

                    SetTokenExpirationInDocumentMetadata(session, persistedGrant);
                }
                else
                {
                    Logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                    token.UpdateEntity(existing);
                }

                try
                {
                    await session.WaitForIndexAndSaveChangesAsync <PersistedGrantIndex>();
                }
                catch (Exception ex)
                {
                    Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
                }
            }
        }
Example #11
0
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await Session.Query <Entities.PersistedGrant>().SingleOrDefaultAsync(x => x.Key == token.Key);

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

                var persistedGrant = token.ToEntity();
                await Session.StoreAsync(persistedGrant);
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
            }

            if (Options.SetTokenExpire && existing.Expiration.HasValue)
            {
                Session.Advanced.GetMetadataFor(existing)[Constants.Documents.Metadata.Expires] = existing.Expiration.Value.ToUniversalTime();
            }

            try
            {
                await Session.SaveChangesAsync();
            }
            catch (ConcurrencyException ex)
            {
                Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }
        }
Example #12
0
        /// <summary>
        /// Stores the grant.
        /// </summary>
        /// <param name="grant">The grant to store.</param>
        public async Task StoreAsync(PersistedGrant grant)
        {
            using (var tx = _session.BeginTransaction())
            {
                try
                {
                    var existingGrant = await _session.GetAsync <Entities.PersistedGrant>(grant.Key);

                    if (existingGrant == null)
                    {
                        _logger.LogDebug("{persistedGrantKey} not found in database. Creating it.", grant.Key);

                        await _session.SaveAsync(grant.ToEntity());
                    }
                    else
                    {
                        _logger.LogDebug("{persistedGrantKey} found in database. Updating it", grant.Key);

                        grant.UpdateEntity(existingGrant);
                        await _session.UpdateAsync(existingGrant);
                    }
                    await tx.CommitAsync();
                }
                catch (HibernateException ex)
                {
                    _logger.LogWarning("exception storing {persistedGrantKey} persisted grant in database: {error}",
                                       grant.Key, ex.Message);
                }
            }
        }
Example #13
0
        /// <summary>
        /// Stores the asynchronous.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public Task StoreAsync(PersistedGrant token)
        {
            var existing = PersistedGrants.FindByKey(token.Key);

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

                existing = token.ToEntity();
            }
            else
            {
                _logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
            }

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

            return(Task.FromResult(0));
        }
Example #14
0
        public async Task StoreAsync(PersistedGrant token)
        {
            var site = _contextAccessor.HttpContext.GetTenant <SiteContext>();

            if (site == null)
            {
                _logger.LogError("sitecontext was null");
                return;
            }
            var _siteId = site.Id.ToString();

            var existing = await _context.PersistedGrants.SingleOrDefaultAsync(x => x.SiteId == _siteId && x.Key == token.Key)
                           .ConfigureAwait(false);

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

            try
            {
                await _context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "StoreAsync");
            }
        }
        public async Task StoreAsync(PersistedGrant token)
        {
            using (var db = _contextFactory())
            {
                var existing = await db.PersistedGrants.SingleOrDefaultAsync(x => x.Key == token.Key);

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

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

                    token.UpdateEntity(existing);
                }

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    //_logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
                }
            }
        }
        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));
        }
Example #17
0
        /// <inheritdoc/>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await UnitOfWork.Query <XpoPersistedGrant>().SingleOrDefaultAsync(x => x.Key == token.Key);

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

                var persistedGrant = token.ToEntity(UnitOfWork);
                await UnitOfWork.SaveAsync(persistedGrant);
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);

                token.UpdateEntity(existing);
                await UnitOfWork.SaveAsync(existing);
            }

            try
            {
                await UnitOfWork.CommitChangesAsync();
            }
            catch (LockingException ex)
            {
                Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
            }
        }
Example #18
0
        public async Task StoreAsync(PersistedGrant grant)
        {
            DocumentSnapshot persistedGrant =
                await GetPersistedGrant(nameof(PersistedGrant.Key), grant.Key).ConfigureAwait(false);

            if (persistedGrant.Exists)
            {
                _logger.LogDebug("{persistedGrantKey} found in database", grant.Key);

                Entities.PersistedGrant existing = persistedGrant.ConvertTo <Entities.PersistedGrant>();

                grant.UpdateEntity(existing);

                await persistedGrant.Reference.SetAsync(existing).ConfigureAwait(false);
            }
            else
            {
                _logger.LogDebug("{persistedGrant} not found in database", grant.Key);

                Entities.PersistedGrant persistedGrand = grant.ToEntity();
                await _context.PersistedGrants.AddAsync(persistedGrand).ConfigureAwait(false);
            }
        }