private static bool IsAccountStoreMapping(IResourceDataResult result)
        {
            var type = result.Type;

            return(typeof(IAccountStoreMapping).IsAssignableFrom(type) ||
                   typeof(IAccountStoreMapping <IApplicationAccountStoreMapping>).IsAssignableFrom(type) ||
                   typeof(IAccountStoreMapping <IOrganizationAccountStoreMapping>).IsAssignableFrom(type));
        }
        private static IResourceDataResult FilterCore(IResourceDataResult result)
        {
            if (typeof(IProviderAccountResult).IsAssignableFrom(result.Type))
            {
                result.Body.Add("isNewAccount", result.HttpStatus == 201);
            }

            return(result);
        }
        private static IResourceDataResult FilterCore(IResourceDataResult result)
        {
            if (typeof(IProviderAccountResult).IsAssignableFrom(result.Type))
            {
                result.Body.Add("isNewAccount", result.HttpStatus == 201);
            }

            return result;
        }
        private static string GetContainerHref(string key, IResourceDataResult result)
        {
            object container;

            if (!result.Body.TryGetValue(key, out container))
            {
                return(null);
            }

            return((container as IEmbeddedProperty)?.Href);
        }
        private void CacheNestedCustomDataUpdates(IResourceDataRequest request, IResourceDataResult result, ILogger logger)
        {
            object customDataObj = null;
            Map    customData    = null;

            if (!request.Properties.TryGetValue(AbstractExtendableInstanceResource.CustomDataPropertyName, out customDataObj))
            {
                return;
            }

            customData = customDataObj as Map;
            if (customData.IsNullOrEmpty())
            {
                return;
            }

            bool creating = request.Action == ResourceAction.Create;

            var parentHref = request.Uri.ResourcePath.ToString();

            if (creating && !result.Body.TryGetValueAsString(AbstractResource.HrefPropertyName, out parentHref))
            {
                return;
            }

            var customDataHref = parentHref + "/customData";

            var dataToCache = this.GetCachedValue(typeof(ICustomData), customDataHref);

            if (!creating && dataToCache == null)
            {
                logger.Trace($"Request {request.Uri} has nested custom data updates, but no authoritative cached custom data exists; aborting", "WriteCacheFilter.CacheNestedCustomDataUpdates");
                return;
            }

            logger.Trace($"Request {request.Uri} has nested custom data updates, updating cached custom data", "WriteCacheFilter.CacheNestedCustomDataUpdates");

            if (dataToCache.IsNullOrEmpty())
            {
                dataToCache = new Dictionary <string, object>();
            }

            foreach (var updatedItem in customData)
            {
                dataToCache[updatedItem.Key] = updatedItem.Value;
            }

            // Ensure the href property exists
            dataToCache[AbstractResource.HrefPropertyName] = customDataHref;

            this.Cache(typeof(ICustomData), dataToCache, logger);
        }
        private async Task UncacheAccountOnEmailVerificationAsync(IResourceDataResult result, CancellationToken cancellationToken)
        {
            object accountHrefRaw = null;
            string accountHref    = null;

            if (!result.Body.TryGetValue(AbstractResource.HrefPropertyName, out accountHrefRaw))
            {
                return;
            }

            accountHref = accountHrefRaw.ToString();
            if (string.IsNullOrEmpty(accountHref))
            {
                return;
            }

            var cache = this.GetAsyncCache(typeof(IAccount));
            await cache.RemoveAsync(this.GetCacheKey(accountHref), cancellationToken).ConfigureAwait(false);
        }
        private void UncacheAccountOnEmailVerification(IResourceDataResult result)
        {
            object accountHrefRaw = null;
            string accountHref    = null;

            if (!result.Body.TryGetValue(AbstractResource.HrefPropertyName, out accountHrefRaw))
            {
                return;
            }

            accountHref = accountHrefRaw.ToString();
            if (string.IsNullOrEmpty(accountHref))
            {
                return;
            }

            var cache = this.GetSyncCache(typeof(IAccount));

            cache.Remove(this.GetCacheKey(accountHref));
        }
        private static bool IsCacheable(IResourceDataRequest request, IResourceDataResult result)
        {
            bool hasData = !result.Body.IsNullOrEmpty();

            return

                // Must be a resource
                (IsResource(result?.Body) &&

                 // Don't cache password reset or email verification requests
                 result.Type != typeof(IPasswordResetToken) &&
                 result.Type != typeof(IEmailVerificationToken) &&
                 result.Type != typeof(IEmailVerificationRequest) &&

                 // Don't cache login attempts
                 result.Type != typeof(IAuthenticationResult) &&

                 // ProviderAccountResults look like IAccounts but should not be cached either
                 result.Type != typeof(IProviderAccountResult));
        }
        private static string GetContainerHref(string key, IResourceDataResult result)
        {
            object container;

            if (!result.Body.TryGetValue(key, out container))
            {
                return null;
            }

            return (container as IEmbeddedProperty)?.Href;
        }
 private static bool IsAccountStoreMapping(IResourceDataResult result)
 {
     var type = result.Type;
     return typeof(IAccountStoreMapping).IsAssignableFrom(type)
         || typeof(IAccountStoreMapping<IApplicationAccountStoreMapping>).IsAssignableFrom(type)
         || typeof(IAccountStoreMapping<IOrganizationAccountStoreMapping>).IsAssignableFrom(type);
 }
        private void UncacheAccountOnEmailVerification(IResourceDataResult result)
        {
            object accountHrefRaw = null;
            string accountHref = null;
            if (!result.Body.TryGetValue(AbstractResource.HrefPropertyName, out accountHrefRaw))
            {
                return;
            }

            accountHref = accountHrefRaw.ToString();
            if (string.IsNullOrEmpty(accountHref))
            {
                return;
            }

            var cache = this.GetSyncCache(typeof(IAccount));
            cache.Remove(this.GetCacheKey(accountHref));
        }
        private void CacheNestedCustomDataUpdates(IResourceDataRequest request, IResourceDataResult result, ILogger logger)
        {
            object customDataObj = null;
            Map customData = null;

            if (!request.Properties.TryGetValue(AbstractExtendableInstanceResource.CustomDataPropertyName, out customDataObj))
            {
                return;
            }

            customData = customDataObj as Map;
            if (customData.IsNullOrEmpty())
            {
                return;
            }

            bool creating = request.Action == ResourceAction.Create;

            var parentHref = request.Uri.ResourcePath.ToString();
            if (creating && !result.Body.TryGetValueAsString(AbstractResource.HrefPropertyName, out parentHref))
            {
                return;
            }

            var customDataHref = parentHref + "/customData";

            var dataToCache = this.GetCachedValue(typeof(ICustomData), customDataHref);
            if (!creating && dataToCache == null)
            {
                logger.Trace($"Request {request.Uri} has nested custom data updates, but no authoritative cached custom data exists; aborting", "WriteCacheFilter.CacheNestedCustomDataUpdates");
                return;
            }

            logger.Trace($"Request {request.Uri} has nested custom data updates, updating cached custom data", "WriteCacheFilter.CacheNestedCustomDataUpdates");

            if (dataToCache.IsNullOrEmpty())
            {
                dataToCache = new Dictionary<string, object>();
            }

            foreach (var updatedItem in customData)
            {
                dataToCache[updatedItem.Key] = updatedItem.Value;
            }

            // Ensure the href property exists
            dataToCache[AbstractResource.HrefPropertyName] = customDataHref;

            this.Cache(typeof(ICustomData), dataToCache, logger);
        }
        private static bool IsCacheable(IResourceDataRequest request, IResourceDataResult result)
        {
            bool hasData = !result.Body.IsNullOrEmpty();

            return

                // Must be a resource
                IsResource(result?.Body) &&

                // Don't cache password reset or email verification requests
                result.Type != typeof(IPasswordResetToken) &&
                result.Type != typeof(IEmailVerificationToken) &&
                result.Type != typeof(IEmailVerificationRequest) &&

                // Don't cache login attempts
                result.Type != typeof(IAuthenticationResult) &&

                // ProviderAccountResults look like IAccounts but should not be cached either
                result.Type != typeof(IProviderAccountResult);
        }
        private async Task UncacheAccountOnEmailVerificationAsync(IResourceDataResult result, CancellationToken cancellationToken)
        {
            object accountHrefRaw = null;
            string accountHref = null;
            if (!result.Body.TryGetValue(AbstractResource.HrefPropertyName, out accountHrefRaw))
            {
                return;
            }

            accountHref = accountHrefRaw.ToString();
            if (string.IsNullOrEmpty(accountHref))
            {
                return;
            }

            var cache = this.GetAsyncCache(typeof(IAccount));
            await cache.RemoveAsync(this.GetCacheKey(accountHref), cancellationToken).ConfigureAwait(false);
        }