public string MakeCacheKey(HttpContext context = null, bool excludeQueryString = false)
        {
            string queryString;

            try
            {
                var uri = new Uri(_serviceOptions.Url);
                queryString = uri.Query;
            }
            catch (Exception)
            {
                queryString = _serviceOptions.Arguments;
            }

            var usernameCacheKey = GetCachePerUserKey(context);

            var data = _serviceOptions.Data == null ? "".GetHashCode() : _serviceOptions.Data.GetHashCode();

            var formData = "";

            if (_serviceOptions.FormData != null)
            {
                foreach (var fd in _serviceOptions.FormData)
                {
                    formData += fd.Value.GetHashCode();
                }
            }

            var extraHeaderData = "";

            if (_serviceOptions.ExtraHeaderData != null)
            {
                foreach (var ehd in _serviceOptions.ExtraHeaderData)
                {
                    extraHeaderData += ehd.Value.GetHashCode();
                }
            }

            var query = "";

            if (!excludeQueryString)
            {
                query = queryString;
            }

            _cacheKeyHasher.ApiName     = ApiName;
            _cacheKeyHasher.Operation   = _serviceOptions.Operation;
            _cacheKeyHasher.UserName    = usernameCacheKey;
            _cacheKeyHasher.OriginalKey = $"{_serviceOptions.Verb}|{_serviceOptions.ClientId}|{_serviceOptions.AccessTokenUrl}|{_serviceOptions.CallBackUrl}|{query}|{data}|{formData}|{extraHeaderData}";

            return(_cacheKeyHasher.GetHashedKey());
        }
Example #2
0
        public string MakeCacheKey(HttpContext context, bool excludeQueryString = false)
        {
            context.IsOutputCachingEnabled(out var config);

            var keyParts = new List <string>
            {
                context.Request.IsHttps ? "https" : "http",
                context.Request.Host.ToString(),
                context.Request.PathBase,
                GetCachePerUserKey(context, config),
                GetQueryStringKey(context, config),
                GetBodyKey(context, config),
                context.Request.Headers[AcceptType]
            };

            var originalKey = string.Join(Delimeter.ToString(), keyParts.Where(x => !string.IsNullOrEmpty(x)));

            return(_cacheKeyHasher.GetHashedKey(config.ApiName, config.ActionName, originalKey, ""));
        }
Example #3
0
        public string MakeCacheKey(HttpActionContext context = null, bool excludeQueryString = false)
        {
            string queryString = "";

            try
            {
                var uri = new Uri(_serviceOptions.Url);
                queryString = uri.Query;
            }
            catch (Exception)
            {
                queryString = _serviceOptions.Arguments;
            }

            var usernameCacheKey = "";

            if (context == null)
            {
                if (_serviceOptions is ServiceConsumptionOptions)
                {
                    context = (_serviceOptions as ServiceConsumptionOptions).ActionExecutedContext;
                }
            }

            if (CachePerUser)
            {
                if (context != null)
                {
                    usernameCacheKey = context.RequestContext.Principal.Identity.IsAuthenticated
                    ? context.RequestContext.Principal.Identity.Name
                    : AnonymusUserName;
                }
                else
                {
                    if (System.Web.HttpContext.Current != null)
                    {
                        usernameCacheKey = System.Web.HttpContext.Current?.User?.Identity?.IsAuthenticated == true
                            ? System.Web.HttpContext.Current.User.Identity.Name
                            : AnonymusUserName;
                    }
                    else
                    {
                        usernameCacheKey = string.IsNullOrEmpty(_serviceOptions.UserName)
                            ? _serviceOptions.UserName
                            : AnonymusUserName;
                    }
                }
            }

            var data = _serviceOptions.Data == null ? "".GetHashCode() : _serviceOptions.Data.GetHashCode();

            var formData = "";

            if (_serviceOptions.FormData != null)
            {
                foreach (var fd in _serviceOptions.FormData)
                {
                    formData += fd.Value.GetHashCode();
                }
            }


            var extraHeaderData = "";

            if (_serviceOptions.ExtraHeaderData != null)
            {
                foreach (var ehd in _serviceOptions.ExtraHeaderData)
                {
                    extraHeaderData += ehd.Value.GetHashCode();
                }
            }

            var query = "";

            if (!excludeQueryString)
            {
                query = queryString;
            }

            _cacheKeyHasher.ApiName     = this.ApiName;
            _cacheKeyHasher.Operation   = _serviceOptions.Operation;
            _cacheKeyHasher.UserName    = usernameCacheKey;
            _cacheKeyHasher.OriginalKey = $"{_serviceOptions.Verb}|{_serviceOptions.ClientId}|{_serviceOptions.AccessTokenUrl}|{_serviceOptions.CallBackUrl}|{query}|{data}|{formData}|{extraHeaderData}";

            return(_cacheKeyHasher.GetHashedKey());
        } //end MakeCacheKey()
Example #4
0
        public override string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, bool excludeQueryString = false)
        {
            var fullName     = context.ControllerContext.ControllerDescriptor.ControllerType.FullName;
            var actionName   = context.ActionDescriptor.ActionName;
            var baseCachekey = context.Request.GetConfiguration().CacheOutputConfiguration().MakeBaseCachekey(fullName, actionName);

            var usernameCacheKey = "";

            if (CachePerUser)
            {
                usernameCacheKey = context.RequestContext.Principal.Identity.IsAuthenticated
                    ? context.RequestContext.Principal.Identity.Name
                    : "anon";

                usernameCacheKey = $"{usernameCacheKey}";
            }

            var    queryStrings = context.ActionArguments.Where(x => x.Value != null).Select(x => x.Key + "=" + GetValue(x.Value));
            string argumentsString;

            if (!excludeQueryString)
            {
                var second = context.Request.GetQueryNameValuePairs().Where(x => x.Key.ToLower() != "callback").Select(x => x.Key + "=" + x.Value);
                argumentsString = "-" + string.Join("&", queryStrings.Union(second));
                var jsonpCallback = GetJsonpCallback(context.Request);
                if (!string.IsNullOrWhiteSpace(jsonpCallback))
                {
                    var callbackString = "callback=" + jsonpCallback;
                    if (argumentsString.Contains("&" + callbackString))
                    {
                        argumentsString = argumentsString.Replace("&" + callbackString, string.Empty);
                    }
                    if (argumentsString.Contains(callbackString + "&"))
                    {
                        argumentsString = argumentsString.Replace(callbackString + "&", string.Empty);
                    }
                    if (argumentsString.Contains("-" + callbackString))
                    {
                        argumentsString = argumentsString.Replace("-" + callbackString, string.Empty);
                    }
                    if (argumentsString.EndsWith("&"))
                    {
                        argumentsString = argumentsString.TrimEnd('&');
                    }
                }
            }
            else
            {
                argumentsString = string.Join("&", queryStrings);
            }

            var headers = "";

            if (HeadersToInlcudeInCacheKey != null && HeadersToInlcudeInCacheKey.Length > 0)
            {
                foreach (var key in HeadersToInlcudeInCacheKey)
                {
                    IEnumerable <string> headerValues = null;
                    if (context.Request.Headers.TryGetValues(key, out headerValues))
                    {
                        headers += key + ":" + string.Join(";", headerValues.ToArray());
                    }
                    else
                    {
                        headers += key + ":null";
                    }
                }
            }

            var postBody = "";

            if (context.Request.Method == HttpMethod.Post ||
                context.Request.Method == HttpMethod.Delete ||
                context.Request.Method == HttpMethod.Put)
            {
                using (var stream = new MemoryStream())
                {
                    if (context.Request.Properties.ContainsKey("MS_HttpContext"))
                    {
                        var httpContext = (HttpContextBase)context.Request.Properties["MS_HttpContext"];
                        httpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
                        httpContext.Request.InputStream.CopyTo(stream);
                        postBody = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                    }
                }
            }

            if (argumentsString == "-")
            {
                argumentsString = string.Empty;
            }

            _cacheKeyHasher.ApiName     = this.ApiName;
            _cacheKeyHasher.Operation   = actionName;
            _cacheKeyHasher.UserName    = usernameCacheKey;
            _cacheKeyHasher.OriginalKey = $"{baseCachekey}|{usernameCacheKey}|{headers}|{argumentsString}|{postBody}|{mediaType}";

            return(_cacheKeyHasher.GetHashedKey());
        }