diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs index 6293c0d034..fb35d142cb 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs @@ -27,8 +27,11 @@ namespace Volo.Abp.Authorization return; } - var authorizationPolicy = await AuthorizationPolicy.CombineAsync(_abpAuthorizationPolicyProvider, - GetAuthorizationDataAttributes(context.Method)); + var authorizationPolicy = await AuthorizationPolicy.CombineAsync( + _abpAuthorizationPolicyProvider, + GetAuthorizationDataAttributes(context.Method) + ); + if (authorizationPolicy == null) { return; diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs index c0f1048d06..66b5f5a2e3 100644 --- a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs @@ -9,16 +9,33 @@ namespace Volo.Abp.Authorization public class Authorization_Tests : AuthorizationTestBase { private readonly IMyAuthorizedService1 _myAuthorizedService1; + private readonly IMySimpleAuthorizedService _simpleAuthorizedService; private readonly IMyAuthorizedServiceWithRole _myAuthorizedServiceWithRole; private readonly IPermissionDefinitionManager _permissionDefinitionManager; public Authorization_Tests() { _myAuthorizedService1 = GetRequiredService(); + _simpleAuthorizedService = GetRequiredService(); _myAuthorizedServiceWithRole = GetRequiredService(); _permissionDefinitionManager = GetRequiredService(); } + [Fact] + public async Task Should_Not_Allow_To_Call_Authorized_Method_For_Anonymous_User() + { + await Assert.ThrowsAsync(async () => + { + await _simpleAuthorizedService.ProtectedByClassAsync(); + }); + } + + [Fact] + public async Task Should_Allow_To_Call_Anonymous_Method_For_Anonymous_User() + { + (await _simpleAuthorizedService.AnonymousAsync()).ShouldBe(42); + } + [Fact] public async Task Should_Not_Allow_To_Call_Method_If_Has_No_Permission_ProtectedByClass() { diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs index 4c693993cf..b3841c4fed 100644 --- a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs @@ -11,6 +11,5 @@ namespace Volo.Abp.Authorization.TestServices Task ProtectedByClass(); Task ProtectedByClassAsync(); - } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMySimpleAuthorizedService.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMySimpleAuthorizedService.cs new file mode 100644 index 0000000000..4b00cdfbae --- /dev/null +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMySimpleAuthorizedService.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Authorization.TestServices +{ + public interface IMySimpleAuthorizedService + { + Task ProtectedByClassAsync(); + + Task AnonymousAsync(); + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MySimpleAuthorizedService.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MySimpleAuthorizedService.cs new file mode 100644 index 0000000000..41cd4e5b85 --- /dev/null +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MySimpleAuthorizedService.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Authorization.TestServices +{ + [Authorize] + public class MySimpleAuthorizedService : IMySimpleAuthorizedService, ITransientDependency + { + public Task ProtectedByClassAsync() + { + return Task.FromResult(42); + } + + [AllowAnonymous] + public Task AnonymousAsync() + { + return Task.FromResult(42); + } + } +} \ No newline at end of file