1
1
using System . Collections . Generic ;
2
2
using System . Threading . Tasks ;
3
+
4
+ using Microsoft . AspNetCore . Authorization ;
5
+ using Microsoft . AspNetCore . Http ;
3
6
using Microsoft . AspNetCore . Mvc ;
7
+ using Microsoft . AspNetCore . Mvc . Authorization ;
4
8
using Microsoft . AspNetCore . Mvc . Filters ;
5
9
using Microsoft . Extensions . DependencyInjection ;
6
10
using Umbraco . Cms . Core . Security ;
@@ -42,6 +46,12 @@ public UmbracoMemberAuthorizeFilter(string allowType, string allowGroup, string
42
46
43
47
public async Task OnAuthorizationAsync ( AuthorizationFilterContext context )
44
48
{
49
+ // Allow Anonymous skips all authorization
50
+ if ( HasAllowAnonymous ( context ) )
51
+ {
52
+ return ;
53
+ }
54
+
45
55
IMemberManager memberManager = context . HttpContext . RequestServices . GetRequiredService < IMemberManager > ( ) ;
46
56
47
57
if ( ! await IsAuthorizedAsync ( memberManager ) )
@@ -51,6 +61,32 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
51
61
}
52
62
}
53
63
64
+ /// <summary>
65
+ /// Copied from https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Authorization/AuthorizeFilter.cs
66
+ /// </summary>
67
+ private bool HasAllowAnonymous ( AuthorizationFilterContext context )
68
+ {
69
+ var filters = context . Filters ;
70
+ for ( var i = 0 ; i < filters . Count ; i ++ )
71
+ {
72
+ if ( filters [ i ] is IAllowAnonymousFilter )
73
+ {
74
+ return true ;
75
+ }
76
+ }
77
+
78
+ // When doing endpoint routing, MVC does not add AllowAnonymousFilters for AllowAnonymousAttributes that
79
+ // were discovered on controllers and actions. To maintain compat with 2.x,
80
+ // we'll check for the presence of IAllowAnonymous in endpoint metadata.
81
+ var endpoint = context . HttpContext . GetEndpoint ( ) ;
82
+ if ( endpoint ? . Metadata ? . GetMetadata < IAllowAnonymous > ( ) != null )
83
+ {
84
+ return true ;
85
+ }
86
+
87
+ return false ;
88
+ }
89
+
54
90
private async Task < bool > IsAuthorizedAsync ( IMemberManager memberManager )
55
91
{
56
92
if ( AllowMembers . IsNullOrWhiteSpace ( ) )
0 commit comments