File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
src/Microsoft.AspNet.Mvc.Core/Filters
test/Microsoft.AspNet.Mvc.Core.Test/Filters Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ public override async Task OnAuthorizationAsync([NotNull] AuthorizationContext c
5050 user . Identity == null ||
5151 ! user . Identity . IsAuthenticated ;
5252
53- if ( userIsAnonymous )
53+ if ( userIsAnonymous && ! HasAllowAnonymous ( context ) )
5454 {
5555 base . Fail ( context ) ;
5656 }
Original file line number Diff line number Diff line change @@ -45,6 +45,26 @@ public async Task Invoke_EmptyClaimsShouldRejectAnonymousUser()
4545 Assert . NotNull ( authorizationContext . Result ) ;
4646 }
4747
48+ [ Fact ]
49+ public async Task Invoke_EmptyClaimsWithAllowAnonymousAttributeShouldNotRejectAnonymousUser ( )
50+ {
51+ // Arrange
52+ var authorizationService = new DefaultAuthorizationService ( Enumerable . Empty < IAuthorizationPolicy > ( ) ) ;
53+ var authorizeAttribute = new AuthorizeAttribute ( ) ;
54+ var authorizationContext = GetAuthorizationContext ( services =>
55+ services . AddInstance < IAuthorizationService > ( authorizationService ) ,
56+ anonymous : true
57+ ) ;
58+
59+ authorizationContext . Filters . Add ( new AllowAnonymousAttribute ( ) ) ;
60+
61+ // Act
62+ await authorizeAttribute . OnAuthorizationAsync ( authorizationContext ) ;
63+
64+ // Assert
65+ Assert . Null ( authorizationContext . Result ) ;
66+ }
67+
4868 [ Fact ]
4969 public async Task Invoke_EmptyClaimsShouldAuthorizeAuthenticatedUser ( )
5070 {
You can’t perform that action at this time.
0 commit comments