public void LocationProperty() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();

            // Act & assert
            MemberHelper.TestPropertyValue(attr, "Location", OutputCacheLocation.ServerAndClient);
        }
        public void NoStoreProperty() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();

            // Act & assert
            MemberHelper.TestBooleanProperty(attr, "NoStore", false /* initialValue */, false /* testDefaultValue */);
        }
        public void DurationProperty() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();

            // Act & assert
            MemberHelper.TestInt32Property(attr, "Duration", 10, 20);
        }
        public void CacheSettingsProperty() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute() {
                CacheProfile = "SomeProfile",
                Duration = 50,
                Location = OutputCacheLocation.Downstream,
                NoStore = true,
                SqlDependency = "SomeSqlDependency",
                VaryByContentEncoding = "SomeContentEncoding",
                VaryByCustom = "SomeCustom",
                VaryByHeader = "SomeHeader",
                VaryByParam = "SomeParam",
            };

            // Act
            OutputCacheParameters cacheSettings = attr.CacheSettings;

            // Assert
            Assert.AreEqual("SomeProfile", cacheSettings.CacheProfile);
            Assert.AreEqual(50, cacheSettings.Duration);
            Assert.AreEqual(OutputCacheLocation.Downstream, cacheSettings.Location);
            Assert.AreEqual(true, cacheSettings.NoStore);
            Assert.AreEqual("SomeSqlDependency", cacheSettings.SqlDependency);
            Assert.AreEqual("SomeContentEncoding", cacheSettings.VaryByContentEncoding);
            Assert.AreEqual("SomeCustom", cacheSettings.VaryByCustom);
            Assert.AreEqual("SomeHeader", cacheSettings.VaryByHeader);
            Assert.AreEqual("SomeParam", cacheSettings.VaryByParam);
        }
        public void CacheProfileProperty() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();

            // Act & assert
            MemberHelper.TestStringProperty(attr, "CacheProfile", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
        public void OnResultExecutingThrowsIfFilterContextIsNull() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    attr.OnResultExecuting(null);
                }, "filterContext");
        }
示例#7
0
 /// <summary>
 /// Registers the global filters.
 /// </summary>
 /// <param name="filters">The filters.</param>
 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
 {
     OutputCacheAttribute cashAttr = new OutputCacheAttribute {
         VaryByParam = "*",
         Duration = 0,
         NoStore = true
     };
     filters.Add(new HandleErrorAttribute());
     filters.Add(new SaveMeMVCFilterAttribute());
     filters.Add(cashAttr);
 }
示例#8
0
        public virtual void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            if (OutputCacheAttribute.IsChildActionCacheActive(filterContext))
            {
                // If a child action cache block is active, we need to fail immediately, even if authorization
                // would have succeeded. The reason is that there's no way to hook a callback to rerun
                // authorization before the fragment is served from the cache, so we can't guarantee that this
                // filter will be re-run on subsequent requests.
                throw new InvalidOperationException(MvcResources.AuthorizeAttribute_CannotUseWithinChildActionCache);
            }

            bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) ||
                                     filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
                typeof(AllowAnonymousAttribute), inherit: true);

            if (skipAuthorization)
            {
                return;
            }

            if (AuthorizeCore(filterContext.HttpContext))
            {
                // ** IMPORTANT **
                // Since we're performing authorization at the action level, the authorization code runs
                // after the output caching module. In the worst case this could allow an authorized user
                // to cause the page to be cached, then an unauthorized user would later be served the
                // cached page. We work around this by telling proxies not to cache the sensitive page,
                // then we hook our custom authorization code into the caching mechanism so that we have
                // the final say on whether a page should be served from the cache.

                HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
            }
            else
            {
                HandleUnauthorizedRequest(filterContext);
            }
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            /* var _context = _filterContext.HttpContext;
             *
             * //redirect if not authenticated
             * if (!_context.Request.IsAuthenticated || (!string.IsNullOrWhiteSpace(SessionId) && _context.Session[SessionId] == null))
             * {
             *   //use the current url for the redirect
             *   if (_context.Request.Url != null)
             *   {
             *       string _redirectOnSuccess = _context.Server.UrlEncode(_context.Request.Url.ToString());
             *       //send them off to the login page
             *       string _redirectUrl = string.Format("?ReturnUrl={0}", _redirectOnSuccess);
             *       string _loginUrl = System.Configuration.ConfigurationManager.AppSettings["LoginUrl"];
             *       _loginUrl = (!string.IsNullOrWhiteSpace(_loginUrl) ? _loginUrl : FormsAuthentication.LoginUrl) + _redirectUrl;
             *
             *       _context.Response.Redirect(_loginUrl, true);
             *   }
             * }*/
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (OutputCacheAttribute.IsChildActionCacheActive(filterContext))
            {
                throw new InvalidOperationException("Authorize Attribute Cannot Use Within Child Action Cache");
            }
            ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
            bool             flag1            = true;
            Type             attributeType1   = typeof(AllowAnonymousAttribute);
            int num1 = flag1 ? 1 : 0;
            int num2;

            if (!actionDescriptor.IsDefined(attributeType1, num1 != 0))
            {
                ControllerDescriptor controllerDescriptor = filterContext.ActionDescriptor.ControllerDescriptor;
                bool flag2          = true;
                Type attributeType2 = typeof(AllowAnonymousAttribute);
                int  num3           = flag2 ? 1 : 0;
                num2 = controllerDescriptor.IsDefined(attributeType2, num3 != 0) ? 1 : 0;
            }
            else
            {
                num2 = 1;
            }
            if (num2 != 0)
            {
                return;
            }

            /* if (AuthorizeCore(filterContext.HttpContext))
             * {
             *   HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
             *   cache.SetProxyMaxAge(new TimeSpan(0L));
             *   throw new NotImplementedException();
             * }
             * HandleUnauthorizedRequest(filterContext);*/

            var _context = filterContext.HttpContext;

            if (!_context.Request.IsAuthenticated || (!string.IsNullOrWhiteSpace(SessionId) && _context.Session[SessionId] == null))
            {
                //use the current url for the redirect
                if (_context.Request.Url != null)
                {
                    string _redirectOnSuccess = _context.Server.UrlEncode(_context.Request.Url.ToString());
                    //send them off to the login page
                    string _redirectUrl = string.Format("?ReturnUrl={0}", _redirectOnSuccess);
                    string _loginUrl    = System.Configuration.ConfigurationManager.AppSettings["LoginUrl"];
                    _loginUrl = (!string.IsNullOrWhiteSpace(_loginUrl) ? _loginUrl : FormsAuthentication.LoginUrl) + _redirectUrl;

                    _context.Response.Redirect(_loginUrl, true);
                }
            }
        }
示例#10
0
        public void OutputCacheDoesNotExecuteIfInChildAction() {
            // Arrange
            OutputCacheAttribute attr = new OutputCacheAttribute();
            Mock<ResultExecutingContext> context = new Mock<ResultExecutingContext>();
            context.Expect(c => c.IsChildAction).Returns(true);
            context.Expect(c => c.Result).Never();

            // Act
            attr.OnResultExecuting(context.Object);

            // Assert
            context.Verify();
        }