Skip to content

Commit b3046a0

Browse files
committed
AllowAnonymous can override AuthorizeAttribute
aspnet#309
1 parent e5aeb73 commit b3046a0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizeAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

test/Microsoft.AspNet.Mvc.Core.Test/Filters/AuthorizeAttributeTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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
{

0 commit comments

Comments
 (0)