File tree Expand file tree Collapse file tree 2 files changed +9
-8
lines changed
4-WebApp-your-API/4-2-B2C/TodoListService Expand file tree Collapse file tree 2 files changed +9
-8
lines changed Original file line number Diff line number Diff line change 8
8
9
9
namespace TodoListService . AuthorizationPolicies
10
10
{
11
+ /// <summary>
12
+ /// Requirement used in authorization policies, to check if the scope claim has at least one of the requirement values.
13
+ /// Since the class also extends AuthorizationHandler, its dependency injection is done out of the box.
14
+ /// </summary>
11
15
public class ScopesRequirement : AuthorizationHandler < ScopesRequirement > , IAuthorizationRequirement
12
16
{
13
- string [ ] _scopes ;
17
+ string [ ] _acceptedScopes ;
14
18
15
- public ScopesRequirement ( params string [ ] scopes )
19
+ public ScopesRequirement ( params string [ ] acceptedScopes )
16
20
{
17
- _scopes = scopes ;
21
+ _acceptedScopes = acceptedScopes ;
18
22
}
19
23
20
24
/// <summary>
21
- /// AuthorizationHandler that will check if the scope claim has the requirement value
25
+ /// AuthorizationHandler that will check if the scope claim has at least one of the requirement values
22
26
/// </summary>
23
27
protected override Task HandleRequirementAsync ( AuthorizationHandlerContext context ,
24
28
ScopesRequirement requirement )
@@ -35,7 +39,7 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
35
39
if ( scopeClaim == null )
36
40
scopeClaim = context ? . User ? . FindFirst ( ClaimConstants . Scope ) ;
37
41
38
- if ( scopeClaim != null && scopeClaim . Value . Split ( ' ' ) . Intersect ( requirement . _scopes ) . Any ( ) )
42
+ if ( scopeClaim != null && scopeClaim . Value . Split ( ' ' ) . Intersect ( requirement . _acceptedScopes ) . Any ( ) )
39
43
{
40
44
context . Succeed ( requirement ) ;
41
45
}
Original file line number Diff line number Diff line change @@ -48,9 +48,6 @@ public void ConfigureServices(IServiceCollection services)
48
48
options . AddPolicy ( "ReadScope" ,
49
49
policy => policy . Requirements . Add ( new ScopesRequirement ( "read" ) ) ) ;
50
50
} ) ;
51
-
52
- // Registering an authorization handler that will check if the scope claim has the requirement specified by the policy
53
- //services.AddSingleton<IAuthorizationHandler, ScopesRequirement>();
54
51
}
55
52
56
53
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
You can’t perform that action at this time.
0 commit comments