public async Task UpdateAsync(Models.Consent consent)
        {
            var item = await context.Consents
                       .Where(x => x.SubjectId == consent.Subject && x.ClientId == consent.ClientId)
                       .FirstOrDefaultAsync();

            if (item == null)
            {
                item = new Entities.Consent
                {
                    SubjectId = consent.Subject,
                    ClientId  = consent.ClientId
                };
                context.Consents.Add(item);
            }

            if (consent.Scopes == null || !consent.Scopes.Any())
            {
                context.Consents.Remove(item);
            }

            item.Scopes = consent.Scopes.StringifyScopes();

            await context.SaveChangesAsync();
        }
        public async Task <Models.Consent> LoadAsync(string subject, string client)
        {
            var found = await context.Consents
                        .Where(x => x.SubjectId == subject && x.ClientId == client)
                        .FirstOrDefaultAsync();

            if (found == null)
            {
                return(null);
            }

            var result = new Models.Consent
            {
                Subject  = found.SubjectId,
                ClientId = found.ClientId,
                Scopes   = found.Scopes.ParseScopes()
            };

            return(result);
        }
        /// <summary>
        /// Persists the subject's consent.
        /// </summary>
        /// <param name="consent">The consent.</param>
        /// <returns></returns>
        public Task UpdateAsync(Consent consent)
        {
            // makes a snapshot as a DB would
            consent.Scopes = consent.Scopes.ToArray();

            var query =
                from c in _consents
                where c.Subject == consent.Subject && c.ClientId == consent.ClientId
                select c;
            var item = query.SingleOrDefault();
            if (item != null)
            {
                item.Scopes = consent.Scopes;
            }
            else
            {
                _consents.Add(consent);
            }
            return Task.FromResult(0);
        }
        public async Task<Models.Consent> LoadAsync(string subject, string client)
        {
            var found = await context.Consents
                .Where(x => x.SubjectId == subject && x.ClientId == client)
                .FirstOrDefaultAsync();

            if (found == null)
            {
                return null;
            }

            var result = new Models.Consent
            {
                Subject = found.SubjectId,
                ClientId = found.ClientId,
                Scopes = found.Scopes.ParseScopes()
            };

            return result;
        }
        /// <summary>
        /// Updates the consent.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="scopes">The scopes.</param>
        /// <returns></returns>
        public virtual async Task UpdateConsentAsync(Client client, ClaimsPrincipal subject, IEnumerable<string> scopes)
        {
            if (client == null) throw new ArgumentNullException("client");
            if (subject == null) throw new ArgumentNullException("subject");

            if (client.AllowRememberConsent)
            {
                var subjectId = subject.GetSubjectId();
                var clientId = client.ClientId;

                if (scopes != null && scopes.Any())
                {
                    var consent = new Consent
                    {
                        Subject = subjectId,
                        ClientId = clientId,
                        Scopes = scopes
                    };
                    await _store.UpdateAsync(consent);
                }
                else
                {
                    await _store.RevokeAsync(subjectId, clientId);
                }
            }
        }