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