public void BuildActionModels_BaseAuthorizeFiltersAreStillValidWhenOverriden()
        {
            // Arrange
            var options = new TestOptionsManager<AuthorizationOptions>();
            options.Value.AddPolicy("Base", policy => policy.RequireClaim("Basic").RequireClaim("Basic2"));
            options.Value.AddPolicy("Derived", policy => policy.RequireClaim("Derived"));

            var provider = new AuthorizationApplicationModelProvider(options);
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager<MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(DerivedController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            var action = Assert.Single(controller.Actions);
            Assert.Equal("Authorize", action.ActionName);
            Assert.Null(action.AttributeRouteModel);
            var authorizeFilters = action.Filters.OfType<AuthorizeFilter>();
            Assert.Single(authorizeFilters);
            Assert.Equal(3, authorizeFilters.First().Policy.Requirements.Count);
        }
 public void OnProvidersExecuted(ApplicationModelProviderContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     // Intentionally empty.
 }
        /// <inheritdoc />
        public virtual void OnProvidersExecuting([NotNull] ApplicationModelProviderContext context)
        {
            foreach (var filter in _globalFilters)
            {
                context.Result.Filters.Add(filter);
            }

            foreach (var controllerType in context.ControllerTypes)
            {
                var controllerModels = BuildControllerModels(controllerType);
                if (controllerModels != null)
                {
                    foreach (var controllerModel in controllerModels)
                    {
                        context.Result.Controllers.Add(controllerModel);
                        controllerModel.Application = context.Result;

                        foreach (var propertyHelper in PropertyHelper.GetProperties(controllerType.AsType()))
                        {
                            var propertyInfo  = propertyHelper.Property;
                            var propertyModel = CreatePropertyModel(propertyInfo);
                            if (propertyModel != null)
                            {
                                propertyModel.Controller = controllerModel;
                                controllerModel.ControllerProperties.Add(propertyModel);
                            }
                        }

                        foreach (var methodInfo in controllerType.AsType().GetMethods())
                        {
                            var actionModels = BuildActionModels(controllerType, methodInfo);
                            if (actionModels != null)
                            {
                                foreach (var actionModel in actionModels)
                                {
                                    actionModel.Controller = controllerModel;
                                    controllerModel.Actions.Add(actionModel);

                                    foreach (var parameterInfo in actionModel.ActionMethod.GetParameters())
                                    {
                                        var parameterModel = CreateParameterModel(parameterInfo);
                                        if (parameterModel != null)
                                        {
                                            parameterModel.Action = actionModel;
                                            actionModel.Parameters.Add(parameterModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public void OnProvidersExecuting(ApplicationModelProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IEnableCorsAttribute enableCors;
            IDisableCorsAttribute disableCors;

            foreach (var controllerModel in context.Result.Controllers)
            {
                enableCors = controllerModel.Attributes.OfType<IEnableCorsAttribute>().FirstOrDefault();
                if (enableCors != null)
                {
                    controllerModel.Filters.Add(new CorsAuthorizationFilterFactory(enableCors.PolicyName));
                }

                disableCors = controllerModel.Attributes.OfType<IDisableCorsAttribute>().FirstOrDefault();
                if (disableCors != null)
                {
                    controllerModel.Filters.Add(new DisableCorsAuthorizationFilter());
                }

                foreach (var actionModel in controllerModel.Actions)
                {
                    enableCors = actionModel.Attributes.OfType<IEnableCorsAttribute>().FirstOrDefault();
                    if (enableCors != null)
                    {
                        actionModel.Filters.Add(new CorsAuthorizationFilterFactory(enableCors.PolicyName));
                    }

                    disableCors = actionModel.Attributes.OfType<IDisableCorsAttribute>().FirstOrDefault();
                    if (disableCors != null)
                    {
                        actionModel.Filters.Add(new DisableCorsAuthorizationFilter());
                    }
                }
            }
        }
        public void CreateControllerModel_DisableCorsAttributeAddsDisableCorsAuthorizationFilter()
        {
            // Arrange
            var corsProvider = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager<MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(DisableCorsController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var model = Assert.Single(context.Result.Controllers);
            Assert.Single(model.Filters, f => f is DisableCorsAuthorizationFilter);
        }
        public void CreateControllerModel_EnableCorsAttributeAddsCorsAuthorizationFilterFactory()
        {
            // Arrange
            var corsProvider = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new MockMvcOptionsAccessor());

            var context = new ApplicationModelProviderContext(new [] { typeof(CorsController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var model = Assert.Single(context.Result.Controllers);
            Assert.Single(model.Filters, f => f is CorsAuthorizationFilterFactory);
        }
        public void CreateControllerModel_AuthorizeAttributeAddsAuthorizeFilter()
        {
            // Arrange
            var provider = new AuthorizationApplicationModelProvider(new TestOptionsManager<AuthorizationOptions>());
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager<MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(AccountController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            Assert.Single(controller.Filters, f => f is AuthorizeFilter);
        }
        public void BuildActionModel_EnableCorsAttributeAddsCorsAuthorizationFilterFactory()
        {
            // Arrange
            var corsProvider = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager<MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(EnableCorsController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            var action = Assert.Single(controller.Actions);
            Assert.Single(action.Filters, f => f is CorsAuthorizationFilterFactory);
        }
        public void BuildActionModel_DisableCorsAttributeAddsDisableCorsAuthorizationFilter()
        {
            // Arrange
            var corsProvider = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new MockMvcOptionsAccessor());

            var context = new ApplicationModelProviderContext(new[] { typeof(DisableCorsActionController).GetTypeInfo() });
            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            var action = Assert.Single(controller.Actions);
            Assert.True(action.Filters.Any(f => f is DisableCorsAuthorizationFilter));
        }
        internal protected ApplicationModel BuildModel()
        {
            var controllerTypes = _controllerTypeProvider.ControllerTypes;
            var context = new ApplicationModelProviderContext(controllerTypes);

            for (var i = 0; i < _applicationModelProviders.Length; i++)
            {
                _applicationModelProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _applicationModelProviders.Length - 1 ; i >= 0; i--)
            {
                _applicationModelProviders[i].OnProvidersExecuted(context);
            }

            return context.Result;
        }
        public void CreateControllerModel_EnableCorsAttributeAddsCorsAuthorizationFilterFactory()
        {
            // Arrange
            var corsProvider    = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new MockMvcOptionsAccessor());

            var context = new ApplicationModelProviderContext(new [] { typeof(CorsController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var model = Assert.Single(context.Result.Controllers);

            Assert.Single(model.Filters, f => f is CorsAuthorizationFilterFactory);
        }
예제 #12
0
        public void CreateControllerModel_AuthorizeAttributeAddsAuthorizeFilter()
        {
            // Arrange
            var provider        = new AuthorizationApplicationModelProvider(new TestOptionsManager <AuthorizationOptions>());
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(AccountController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Single(controller.Filters, f => f is AuthorizeFilter);
        }
예제 #13
0
        public void CreateControllerModel_DisableCorsAttributeAddsDisableCorsAuthorizationFilter()
        {
            // Arrange
            var corsProvider    = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(DisableCorsController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var model = Assert.Single(context.Result.Controllers);

            Assert.Single(model.Filters, f => f is DisableCorsAuthorizationFilter);
        }
        public void BuildActionModel_DisableCorsAttributeAddsDisableCorsAuthorizationFilter()
        {
            // Arrange
            var corsProvider    = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new MockMvcOptionsAccessor());

            var context = new ApplicationModelProviderContext(new[] { typeof(DisableCorsActionController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            var action     = Assert.Single(controller.Actions);

            Assert.True(action.Filters.Any(f => f is DisableCorsAuthorizationFilter));
        }
예제 #15
0
        public void BuildActionModel_EnableCorsAttributeAddsCorsAuthorizationFilterFactory()
        {
            // Arrange
            var corsProvider    = new CorsApplicationModelProvider();
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(EnableCorsController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            corsProvider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);
            var action     = Assert.Single(controller.Actions);

            Assert.Single(action.Filters, f => f is CorsAuthorizationFilterFactory);
        }
        public void OnProvidersExecuting(ApplicationModelProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            AuthorizationPolicy policy;

            foreach (var controllerModel in context.Result.Controllers)
            {
                policy = AuthorizationPolicy.Combine(
                    _authorizationOptions,
                    controllerModel.Attributes.OfType <IAuthorizeData>());
                if (policy != null)
                {
                    controllerModel.Filters.Add(new AuthorizeFilter(policy));
                }

                foreach (var attribute in controllerModel.Attributes.OfType <IAllowAnonymous>())
                {
                    controllerModel.Filters.Add(new AllowAnonymousFilter());
                }

                foreach (var actionModel in controllerModel.Actions)
                {
                    policy = AuthorizationPolicy.Combine(
                        _authorizationOptions,
                        actionModel.Attributes.OfType <IAuthorizeData>());
                    if (policy != null)
                    {
                        actionModel.Filters.Add(new AuthorizeFilter(policy));
                    }

                    foreach (var attribute in actionModel.Attributes.OfType <IAllowAnonymous>())
                    {
                        actionModel.Filters.Add(new AllowAnonymousFilter());
                    }
                }
            }
        }
        public void OnProvidersExecuting(ApplicationModelProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            AuthorizationPolicy policy;

            foreach (var controllerModel in context.Result.Controllers)
            {
                policy = AuthorizationPolicy.Combine(
                    _authorizationOptions,
                    controllerModel.Attributes.OfType<IAuthorizeData>());
                if (policy != null)
                {
                    controllerModel.Filters.Add(new AuthorizeFilter(policy));
                }

                foreach (var attribute in controllerModel.Attributes.OfType<IAllowAnonymous>())
                {
                    controllerModel.Filters.Add(new AllowAnonymousFilter());
                }

                foreach (var actionModel in controllerModel.Actions)
                {
                    policy = AuthorizationPolicy.Combine(
                        _authorizationOptions,
                        actionModel.Attributes.OfType<IAuthorizeData>());
                    if (policy != null)
                    {
                        actionModel.Filters.Add(new AuthorizeFilter(policy));
                    }

                    foreach (var attribute in actionModel.Attributes.OfType<IAllowAnonymous>())
                    {
                        actionModel.Filters.Add(new AllowAnonymousFilter());
                    }
                }
            }
        }
        public void OnProvidersExecuting_AddsControllerProperties()
        {
            // Arrange
            var builder  = new TestApplicationModelProvider();
            var typeInfo = typeof(ModelBinderController).GetTypeInfo();

            var context = new ApplicationModelProviderContext(new[] { typeInfo });

            // Act
            builder.OnProvidersExecuting(context);

            // Assert
            var model = Assert.Single(context.Result.Controllers);

            Assert.Equal(2, model.ControllerProperties.Count);
            Assert.Equal("Bound", model.ControllerProperties[0].PropertyName);
            Assert.Equal(BindingSource.Query, model.ControllerProperties[0].BindingInfo.BindingSource);
            Assert.NotNull(model.ControllerProperties[0].Controller);
            var attribute = Assert.Single(model.ControllerProperties[0].Attributes);

            Assert.IsType <FromQueryAttribute>(attribute);
        }
 /// <inheritdoc />
 public virtual void OnProvidersExecuted([NotNull] ApplicationModelProviderContext context)
 {
     // Intentionally empty.
 }
 public void OnProvidersExecuted(ApplicationModelProviderContext context)
 {
     // Intentionally empty.
 }
 public void OnProvidersExecuted(ApplicationModelProviderContext context)
 {
     // Intentionally empty.
 }