Exemplo n.º 1
0
    public async Task CombinedAuthorizeFilter_AlwaysCalledWithNonDefaultProvider()
    {
        // Arrange
        var applicationModelProviderContext = GetProviderContext(typeof(AuthorizeController));

        var policyProvider = new TestAuthorizationPolicyProvider();

        var controller      = Assert.Single(applicationModelProviderContext.Result.Controllers);
        var action          = Assert.Single(controller.Actions);
        var authorizeData   = action.Attributes.OfType <AuthorizeAttribute>();
        var authorizeFilter = new AuthorizeFilter(policyProvider, authorizeData);

        var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ControllerActionDescriptor());

        var authorizationFilterContext = new AuthorizationFilterContext(actionContext, action.Filters);

        authorizationFilterContext.Filters.Add(authorizeFilter);

        var secondFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(a => true).Build());

        authorizationFilterContext.Filters.Add(secondFilter);

        var thirdFilter = new AuthorizeFilter(policyProvider, authorizeData);

        authorizationFilterContext.Filters.Add(thirdFilter);

        // Act
        await thirdFilter.OnAuthorizationAsync(authorizationFilterContext);

        await thirdFilter.OnAuthorizationAsync(authorizationFilterContext);

        // Assert
        Assert.Equal(4, policyProvider.GetPolicyCount);
    }
Exemplo n.º 2
0
        public async Task AuthorizeFilter_CalledTwiceWithNonDefaultProvider()
        {
            // Arrange
            var applicationModelProviderContext = new ApplicationModelProviderContext(
                new[] { typeof(AuthorizeController).GetTypeInfo() });

            var policyProvider  = new TestAuthorizationPolicyProvider();
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            defaultProvider.OnProvidersExecuting(applicationModelProviderContext);

            var controller      = Assert.Single(applicationModelProviderContext.Result.Controllers);
            var action          = Assert.Single(controller.Actions);
            var authorizeData   = action.Attributes.OfType <AuthorizeAttribute>();
            var authorizeFilter = new AuthorizeFilter(policyProvider, authorizeData);

            var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ControllerActionDescriptor());

            var authorizationFilterContext = new AuthorizationFilterContext(actionContext, action.Filters);

            // Act
            await authorizeFilter.OnAuthorizationAsync(authorizationFilterContext);

            await authorizeFilter.OnAuthorizationAsync(authorizationFilterContext);

            // Assert
            Assert.Equal(2, policyProvider.GetPolicyCount);
        }