1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Net . Http ;
4
5
using System . Net . Http . Headers ;
5
6
using System . Security . Claims ;
6
7
using System . Threading . Tasks ;
7
8
using Microsoft . AspNetCore . Authentication . OpenIdConnect ;
8
9
using Microsoft . AspNetCore . Authorization ;
10
+ using Microsoft . Extensions . DependencyInjection ;
9
11
using Microsoft . Extensions . Options ;
10
12
using Microsoft . Graph ;
11
13
using Microsoft . Identity . Web ;
@@ -18,16 +20,16 @@ public class AllowListAuthorizationHandler : AuthorizationHandler<AllowListRequi
18
20
private const string AllowedGroupClaimName = "ExplorePackages.AllowedGroup" ;
19
21
private const string HttpContextKeyForJwt = "JwtSecurityTokenUsedToCallWebAPI" ;
20
22
21
- private readonly GraphServiceClient _graphServiceClient ;
23
+ private readonly IServiceProvider _serviceProvider ;
22
24
private readonly bool _restrictUsers ;
23
25
private readonly Dictionary < string , HashSet < string > > _allowedUsers ;
24
26
private readonly Dictionary < string , HashSet < string > > _allowedGroups ;
25
27
26
28
public AllowListAuthorizationHandler (
27
- GraphServiceClient graphServiceClient ,
29
+ IServiceProvider serviceProvider ,
28
30
IOptions < ExplorePackagesWebsiteSettings > options )
29
31
{
30
- _graphServiceClient = graphServiceClient ;
32
+ _serviceProvider = serviceProvider ;
31
33
_restrictUsers = options . Value . RestrictUsers ;
32
34
_allowedUsers = TenantToObjectIds ( options . Value . AllowedUsers ) ;
33
35
_allowedGroups = TenantToObjectIds ( options . Value . AllowedGroups ) ;
@@ -111,7 +113,8 @@ public async Task AddAllowedGroupClaimsAsync(TokenValidatedContext context)
111
113
// Source: https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/ef20861535add11f5d37e25228379c8dfc5d1796/5-WebApp-AuthZ/5-2-Groups/Services/MicrosoftGraph-Rest/GraphHelper.cs
112
114
context . HttpContext . Items [ HttpContextKeyForJwt ] = context . SecurityToken ;
113
115
114
- var memberGroups = await _graphServiceClient
116
+ var memberGroups = await _serviceProvider
117
+ . GetRequiredService < GraphServiceClient > ( )
115
118
. Me
116
119
. CheckMemberGroups ( objectIds )
117
120
. Request ( )
0 commit comments