Example #1
0
 public virtual void OnAuthorization(AuthorizationContext filterContext)
 {
     if (filterContext == null)
     {
         throw new ArgumentNullException("filterContext");
     }
     if (this.AuthorizeCore(filterContext.HttpContext, filterContext.AllowAnonymous))
     {
         HttpCachePolicy cache = filterContext.HttpContext.Response.Cache;
         cache.SetProxyMaxAge(new TimeSpan(0L));
         cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), filterContext.AllowAnonymous);
         return;
     }
     this.HandleUnauthorizedRequest(filterContext);
 }
Example #2
0
        public virtual void OnAuthorization(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            /*
             * if ((httpContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ? true : filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)))
             * {
             *  return;
             * }
             */

            if (!this.AuthorizeCore(httpContext))
            {
                this.HandleUnauthorizedRequest(httpContext);
                return;
            }
            HttpCachePolicy cache = httpContext.Response.Cache;

            cache.SetProxyMaxAge(new TimeSpan((long)0));
            cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), null);
        }
Example #3
0
 /// <summary>
 ///     Registers a validation callback for the current response.
 /// </summary>
 /// <param name="handler">The <see cref="T:System.Web.HttpCacheValidateHandler" /> value. </param>
 /// <param name="data">
 ///     The arbitrary user-supplied data that is passed back to the
 ///     <see cref="M:System.Web.HttpCachePolicy.AddValidationCallback(System.Web.HttpCacheValidateHandler,System.Object)" />
 ///     delegate.
 /// </param>
 /// <exception cref="T:System.ArgumentNullException">The specified <paramref name="handler" /> is null. </exception>
 public void AddValidationCallback(HttpCacheValidateHandler handler, object data)
 {
     policy.AddValidationCallback(handler, data);
 }
Example #4
0
 public override void AddValidationCallback(HttpCacheValidateHandler handler, object data)
 {
     _httpCachePolicy.AddValidationCallback(handler, data);
 }
            /// <summary>
            /// Sets the output cache policy for the specified domain operation entry.
            /// </summary>
            /// <param name="context">Current HttpContext</param>
            /// <param name="domainOperationEntry">The domain operation entry we need to define the cache policy for.</param>
            private static void SetOutputCachingPolicy(HttpContext context, DomainOperationEntry domainOperationEntry)
            {
                if (context == null)
                {
                    return;
                }

                if (QueryOperationInvoker.SupportsCaching(context, domainOperationEntry))
                {
                    OutputCacheAttribute outputCacheInfo = QueryOperationInvoker.GetOutputCacheInformation(domainOperationEntry);
                    if (outputCacheInfo != null)
                    {
                        HttpCachePolicy policy = context.Response.Cache;
                        if (outputCacheInfo.UseSlidingExpiration)
                        {
                            policy.SetSlidingExpiration(true);
                            policy.AddValidationCallback((HttpCacheValidateHandler) delegate(HttpContext c, object d, ref HttpValidationStatus status)
                            {
                                SlidingExpirationValidator validator = (SlidingExpirationValidator)d;
                                if (validator.IsValid())
                                {
                                    status = HttpValidationStatus.Valid;
                                }
                                else
                                {
                                    status = HttpValidationStatus.Invalid;
                                }
                            }, new SlidingExpirationValidator(outputCacheInfo.Duration));
                        }

                        if (outputCacheInfo.Duration > -1)
                        {
                            // When sliding expiration is set, ASP.NET will use the following to figure out the sliding expiration delta.
                            policy.SetExpires(DateTime.UtcNow.AddSeconds(outputCacheInfo.Duration));
                            policy.SetMaxAge(TimeSpan.FromSeconds(outputCacheInfo.Duration));
                            policy.SetValidUntilExpires(/* validUntilExpires */ true);
                        }

                        policy.SetCacheability(QueryOperationInvoker.GetCacheability(outputCacheInfo.Location));

                        if (!String.IsNullOrEmpty(outputCacheInfo.SqlCacheDependencies))
                        {
                            // Syntax is <databaseEntry>:<tableName>[;<databaseEntry>:<tableName>]*.
                            string[] dependencies = outputCacheInfo.SqlCacheDependencies.Split(QueryOperationInvoker.semiColonDelimiter, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string dependency in dependencies)
                            {
                                string[] dependencyTokens = dependency.Split(QueryOperationInvoker.colonDelimiter, StringSplitOptions.RemoveEmptyEntries);
                                if (dependencyTokens.Length != 2)
                                {
                                    throw new InvalidOperationException(Resource.DomainService_InvalidSqlDependencyFormat);
                                }

                                context.Response.AddCacheDependency(new SqlCacheDependency(dependencyTokens[0], dependencyTokens[1]));
                            }
                        }

                        if (!String.IsNullOrEmpty(outputCacheInfo.VaryByHeaders))
                        {
                            string[] headers = outputCacheInfo.VaryByHeaders.Split(QueryOperationInvoker.semiColonDelimiter, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string header in headers)
                            {
                                policy.VaryByHeaders[header] = true;
                            }
                        }

                        // The cache is based on the values of the domain operation entry's parameters.
                        foreach (DomainOperationParameter pi in domainOperationEntry.Parameters)
                        {
                            policy.VaryByParams[pi.Name] = true;
                        }

                        // We don't cache when query parameters are used. We need to vary by query parameters
                        // though such that we can intercept requests with query parameters and by-pass the cache.
                        foreach (string queryParameter in supportedQueryParameters)
                        {
                            policy.VaryByParams[queryParameter] = true;
                        }

                        return;
                    }
                }

                // By default, don't let clients/proxies cache anything.
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }
Example #6
0
        public void Deny_Unrestricted()
        {
            HttpCachePolicy cache = response.Cache;

            Assert.IsNotNull(cache.VaryByHeaders, "VaryByHeaders");
            Assert.IsNotNull(cache.VaryByParams, "VaryByParams");
            cache.AddValidationCallback(new HttpCacheValidateHandler(Validate), null);
            cache.AppendCacheExtension("mono");
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetCacheability(HttpCacheability.NoCache, "mono");
            cache.SetETag("etag");
            try
            {
                cache.SetETagFromFileDependencies();
            }
            catch (TypeInitializationException)
            {
                // 1.1 tries to initialize HttpRuntime
            }
            catch (InvalidOperationException)
            {
                // expected
            }
            cache.SetExpires(DateTime.MinValue);
            cache.SetLastModified(DateTime.Now);
            try
            {
                cache.SetLastModifiedFromFileDependencies();
            }
            catch (InvalidOperationException)
            {
                // expected
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetMaxAge(TimeSpan.FromTicks(1000));
            try
            {
                cache.SetNoServerCaching();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            try
            {
                cache.SetNoStore();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            try
            {
                cache.SetNoTransforms();
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetProxyMaxAge(TimeSpan.FromTicks(2000));
            cache.SetRevalidation(HttpCacheRevalidation.None);
            cache.SetSlidingExpiration(true);
            try
            {
                cache.SetValidUntilExpires(true);
            }
            catch (NotImplementedException)
            {
                // mono
            }
            cache.SetVaryByCustom("custom");
            cache.SetAllowResponseInBrowserHistory(true);

#if NET_2_0
            try
            {
                cache.SetOmitVaryStar(false);
            }
            catch (NotImplementedException)
            {
                // mono
            }
#endif
        }