public async Task AuthorizeFilterWillCallPolicyProviderOnAuthorization() { // Arrange var policyProvider = new Mock <IAuthorizationPolicyProvider>(); var getPolicyCount = 0; policyProvider.Setup(p => p.GetPolicyAsync(It.IsAny <string>())).ReturnsAsync(new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()) .Callback(() => getPolicyCount++); var authorizeFilter = new AuthorizeFilter(policyProvider.Object, new AuthorizeAttribute[] { new AuthorizeAttribute("whatever") }); var authorizationContext = GetAuthorizationContext(); authorizationContext.Filters.Add(authorizeFilter); // Act & Assert await authorizeFilter.OnAuthorizationAsync(authorizationContext); Assert.Equal(1, getPolicyCount); Assert.Null(authorizationContext.Result); await authorizeFilter.OnAuthorizationAsync(authorizationContext); Assert.Equal(2, getPolicyCount); Assert.Null(authorizationContext.Result); await authorizeFilter.OnAuthorizationAsync(authorizationContext); Assert.Equal(3, getPolicyCount); Assert.Null(authorizationContext.Result); // Make sure we don't cache the policy Assert.Null(authorizeFilter.Policy); }
public async Task AuthorizeFilter_CalledTwiceWithDefaultProvider() { // Arrange var applicationModelProviderContext = GetProviderContext(typeof(AuthorizeController)); var policy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build(); var policyProvider = new Mock <DefaultAuthorizationPolicyProvider>(Options.Create <AuthorizationOptions>(new AuthorizationOptions())); var getPolicyCalled = 0; policyProvider.Setup(p => p.GetPolicyAsync(It.IsAny <string>())).Callback(() => getPolicyCalled++).ReturnsAsync(policy); var controller = Assert.Single(applicationModelProviderContext.Result.Controllers); var action = Assert.Single(controller.Actions); var authorizeData = action.Attributes.OfType <AuthorizeAttribute>(); var authorizeFilter = new AuthorizeFilter(policyProvider.Object, authorizeData); var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ControllerActionDescriptor()); var authorizationFilterContext = new AuthorizationFilterContext(actionContext, new[] { authorizeFilter }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationFilterContext); await authorizeFilter.OnAuthorizationAsync(authorizationFilterContext); // Assert Assert.Equal(2, getPolicyCalled); }
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); }
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); }
public async Task Invoke_FailedContextShouldNotCheckPermission() { // Arrange bool authorizationServiceIsCalled = false; var authorizationService = new Mock <IAuthorizationService>(); authorizationService .Setup(x => x.AuthorizeAsync(null, null, "CanViewComment")) .Returns(() => { authorizationServiceIsCalled = true; return(Task.FromResult(true)); }); var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddInstance(authorizationService.Object) ); authorizationContext.Result = new HttpUnauthorizedResult(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.False(authorizationServiceIsCalled); }
public async Task AuthorizationFilterCombinesMultipleFiltersWithPolicyProvider() { // Arrange var authorizeFilter = new AuthorizeFilter(new TestPolicyProvider(), new IAuthorizeData[] { new AuthorizeAttribute { Policy = "true" }, new AuthorizeAttribute { Policy = "false" } }); var authorizationContext = GetAuthorizationContext(anonymous: false); // Effective policy should fail, if both are combined authorizationContext.Filters.Add(authorizeFilter); var secondFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(a => true).Build()); authorizationContext.Filters.Add(secondFilter); // Act await secondFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ForbidResult>(authorizationContext.Result); }
public async Task AuthorizationFilterCombinesMultipleFiltersWithDifferentPolicyProvider() { // Arrange var testProvider1 = new TestPolicyProvider(); var testProvider2 = new TestPolicyProvider(); var authorizeFilter = new AuthorizeFilter(testProvider1, new IAuthorizeData[] { new AuthorizeAttribute { Policy = "true" }, new AuthorizeAttribute { Policy = "false" } }); var authorizationContext = GetAuthorizationContext(anonymous: false); // Effective policy should fail, if both are combined authorizationContext.Filters.Add(authorizeFilter); var secondFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(a => true).Build()); authorizationContext.Filters.Add(secondFilter); var thirdFilter = new AuthorizeFilter(testProvider2, new IAuthorizeData[] { new AuthorizeAttribute(policy: "something") }); authorizationContext.Filters.Add(thirdFilter); // Act await thirdFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ForbidResult>(authorizationContext.Result); Assert.Equal(2, testProvider1.GetPolicyCalls); Assert.Equal(1, testProvider2.GetPolicyCalls); // Make sure the policy calls are not cached await thirdFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ForbidResult>(authorizationContext.Result); Assert.Equal(4, testProvider1.GetPolicyCalls); Assert.Equal(2, testProvider2.GetPolicyCalls); }
public async Task AuthZResourceShouldBeAuthorizationFilterContext() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(c => c.Resource is AuthorizationFilterContext).Build()); var authorizationContext = GetAuthorizationContext(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_SingleValidClaimShouldSucceed() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireClaim("Permission", "CanViewComment", "CanViewPage").Build()); var authorizationContext = GetAuthorizationContext(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task AuthorizeFilterCanAuthorizeNullUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); var authorizationContext = GetAuthorizationContext(anonymous: true); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_ValidClaimShouldNotFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireClaim("Permission", "CanViewPage").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsShouldAuthorizeAuthenticatedUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_RequireUnknownRoleShouldFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Wut").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_RequireAdminAndUserRoleWithNoPolicyShouldSucceed() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Administrator").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_ValidClaimShouldNotFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireClaim("Permission", "CanViewPage").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsShouldAuthorizeAuthenticatedUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_CanFilterToOnlyBearerScheme() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Bearer") .RequireClaim("Permission", "CanViewPage") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_FailWhenLookingForClaimInOtherIdentity() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_RequireUnknownRoleShouldForbid() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Wut").Build()); var authorizationContext = GetAuthorizationContext(); authorizationContext.Filters.Add(authorizeFilter); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ForbidResult>(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsWithAllowAnonymousAttributeShouldNotRejectAnonymousUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(anonymous: true); authorizationContext.Filters.Add(new AllowAnonymousFilter()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsShouldChallengeAnonymousUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(anonymous: true); authorizationContext.Filters.Add(authorizeFilter); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ChallengeResult>(authorizationContext.Result); }
public async Task Invoke_AuthSchemesFailShouldSetEmptyPrincipalOnContext() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Fails") .RequireAuthenticatedUser() .Build()); var authorizationContext = GetAuthorizationContext(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.HttpContext.User?.Identity); }
public async Task Invoke_EmptyClaimsShouldRejectAnonymousUser() { // Arrange var authorizationOptions = new AuthorizationOptions(); var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization(), anonymous: true); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsWithAllowAnonymousAttributeShouldNotRejectAnonymousUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization(), anonymous: true); authorizationContext.Filters.Add(new AllowAnonymousAttribute()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_RequireAdminRoleShouldFailWithNoHandlers() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Administrator").Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddOptions(); services.AddTransient <IAuthorizationService, DefaultAuthorizationService>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_RequireAdminRoleButFailPolicyShouldFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireRole("Administrator") .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_CanLookingForClaimsInMultipleIdentities() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Basic", "Bearer") .RequireClaim("Permission", "CanViewComment") .RequireClaim("Permission", "CupBearer") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_SingleValidClaimShouldSucceed() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireClaim("Permission", "CanViewComment", "CanViewPage").Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddAuthorization(); services.AddTransient <IAuthorizationHandler, DenyAnonymousAuthorizationHandler>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_EmptyClaimsShouldAuthorizeAuthenticatedUser() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddAuthorization(); services.AddTransient <IAuthorizationHandler, DenyAnonymousAuthorizationHandler>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task AuthorizeFilter_CreatedWithPolicy_ThrowsWhenOnAuthorizationAsyncIsCalled() { // Arrange var authorizeFilter = new AuthorizeFilter(new[] { new AuthorizeAttribute() }); var authorizationContext = GetAuthorizationContext(); authorizationContext.Filters.Add(authorizeFilter); var expected = "An AuthorizationPolicy cannot be created without a valid instance of " + "IAuthorizationPolicyProvider."; // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>( () => authorizeFilter.OnAuthorizationAsync(authorizationContext)); Assert.Equal(expected, ex.Message); }
public async Task Invoke_InvalidClaimShouldForbid() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(); authorizationContext.Filters.Add(authorizeFilter); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.IsType <ForbidResult>(authorizationContext.Result); }
public async Task AuthorizationFilterIgnoresFirstFilterWhenCombining() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(a => false).Build()); var authorizationContext = GetAuthorizationContext(anonymous: false); // Effective policy should fail, if both are combined authorizationContext.Filters.Add(authorizeFilter); var secondFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAssertion(a => false).Build()); authorizationContext.Filters.Add(secondFilter); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_FailWhenLookingForClaimInOtherIdentity() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public override async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var realAuthorizeFilter = new AuthorizeFilter("P2"); await realAuthorizeFilter.OnAuthorizationAsync(context); }
public async Task Invoke_RequireAdminRoleShouldFailWithNoHandlers() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Administrator").Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddOptions(); services.AddTransient<IAuthorizationService, DefaultAuthorizationService>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_RequireUnknownRoleShouldFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Wut").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_RequireAdminAndUserRoleWithNoPolicyShouldSucceed() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireRole("Administrator").Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_RequireAdminRoleButFailPolicyShouldFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireRole("Administrator") .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_FailedContextShouldNotCheckPermission() { // Arrange bool authorizationServiceIsCalled = false; var authorizationService = new Mock<IAuthorizationService>(); authorizationService .Setup(x => x.AuthorizeAsync(null, null, "CanViewComment")) .Returns(() => { authorizationServiceIsCalled = true; return Task.FromResult(true); }); var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder() .RequireClaim("Permission", "CanViewComment") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddInstance(authorizationService.Object) ); authorizationContext.Result = new HttpUnauthorizedResult(); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.False(authorizationServiceIsCalled); }
public async Task Invoke_SingleValidClaimShouldSucceed() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireClaim("Permission", "CanViewComment", "CanViewPage").Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddAuthorization(); services.AddTransient<IAuthorizationHandler, DenyAnonymousAuthorizationHandler>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.Null(authorizationContext.Result); }
public async Task Invoke_CanFilterToOnlyBearerScheme() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Bearer") .RequireClaim("Permission", "CanViewPage") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_AuthSchemesFailShouldSetEmptyPrincipalOnContext() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Fails") .RequireAuthenticatedUser() .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.HttpContext.User?.Identity); }
public async Task Invoke_CanLookingForClaimsInMultipleIdentities() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Basic", "Bearer") .RequireClaim("Permission", "CanViewComment") .RequireClaim("Permission", "CupBearer") .Build()); var authorizationContext = GetAuthorizationContext(services => services.AddAuthorization()); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }
public async Task Invoke_EmptyPolicyWillFail() { // Arrange var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder().Build()); var authorizationContext = GetAuthorizationContext(services => { services.AddAuthorization(); services.AddTransient<IAuthorizationHandler, DenyAnonymousAuthorizationHandler>(); }); // Act await authorizeFilter.OnAuthorizationAsync(authorizationContext); // Assert Assert.NotNull(authorizationContext.Result); }