@@ -24,32 +24,45 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
24
24
// Checks if the incoming token contained a 'Group Overage' claim.
25
25
if ( context . Principal . Claims . Any ( x => x . Type == "hasgroups" || ( x . Type == "_claim_names" && x . Value == "{\" groups\" :\" src1\" }" ) ) )
26
26
{
27
- // For this API call to succeed , the app should have permission 'GroupMember.Read.All' granted .
28
- var graph = context . HttpContext . RequestServices . GetService < GraphServiceClient > ( ) ;
27
+ // Before instatntiating GraphServiceClient , the app should have granted admin consent for 'GroupMember.Read.All' permission .
28
+ var graphClient = context . HttpContext . RequestServices . GetService < GraphServiceClient > ( ) ;
29
29
30
- if ( graph == null )
30
+ if ( graphClient == null )
31
31
{
32
32
Console . WriteLine ( "No service for type 'Microsoft.Graph.GraphServiceClient' has been registered in the Startup." ) ;
33
33
}
34
+
35
+ // Checks if the SecurityToken is not null.
36
+ // For the Web App, SecurityToken contains value of the ID Token.
34
37
else if ( context . SecurityToken != null )
35
38
{
36
- // Check if an on-behalf-of all was made to a Web API
39
+ // Checks if 'JwtSecurityTokenUsedToCallWebAPI' key already exists.
40
+ // This key is required to acquire Access Token for Graph Service Client.
37
41
if ( ! context . HttpContext . Items . ContainsKey ( "JwtSecurityTokenUsedToCallWebAPI" ) )
38
42
{
39
- // extract the cached AT that was presented to the Web API
43
+ // For Web App, access token is retrieved using account identifier. But at this point account identifier is null.
44
+ // So, SecurityToken is saved in 'JwtSecurityTokenUsedToCallWebAPI' key.
45
+ // The key is then used to get the Access Token on-behalf of user.
40
46
context . HttpContext . Items . Add ( "JwtSecurityTokenUsedToCallWebAPI" , context . SecurityToken as JwtSecurityToken ) ;
41
47
}
42
48
43
- // We do not want to pull all attributes of a group from MS Graph, so we use a 'select' to just pick the ones we need .
49
+ // The properties that we want to retrieve from MemberOf endpoint .
44
50
string select = "id,displayName,onPremisesNetBiosName,onPremisesDomainName,onPremisesSamAccountNameonPremisesSecurityIdentifier" ;
45
-
46
- // TODO: this line needs a try-catch, with the exception error message being "A call to Microsoft Graph failed, the error is <whatever>"
47
- // Make a Graph call to get groups and directory roles that the user is a direct member of.
48
- var memberPage = await graph . Me . MemberOf . Request ( ) . Select ( select ) . GetAsync ( ) . ConfigureAwait ( false ) ;
49
-
51
+
52
+ IUserMemberOfCollectionWithReferencesPage memberPage = new UserMemberOfCollectionWithReferencesPage ( ) ;
53
+ try
54
+ {
55
+ //Request to get groups and directory roles that the user is a direct member of.
56
+ memberPage = await graphClient . Me . MemberOf . Request ( ) . Select ( select ) . GetAsync ( ) . ConfigureAwait ( false ) ;
57
+ }
58
+ catch ( Exception graphEx )
59
+ {
60
+ var exMsg = graphEx . InnerException != null ? graphEx . InnerException . Message : graphEx . Message ;
61
+ Console . WriteLine ( "Call to Microsoft Graph failed: " + exMsg ) ;
62
+ }
50
63
if ( memberPage ? . Count > 0 )
51
64
{
52
- // If the result is paginated, this method will process all the pages for us .
65
+ // There is a limit to number of groups returned, below method make calls to Microsoft graph to get all the groups .
53
66
var allgroups = ProcessIGraphServiceMemberOfCollectionPage ( memberPage ) ;
54
67
55
68
if ( allgroups ? . Count > 0 )
@@ -85,11 +98,12 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
85
98
}
86
99
finally
87
100
{
101
+ // Checks if the key 'JwtSecurityTokenUsedToCallWebAPI' exists.
88
102
if ( context . HttpContext . Items . ContainsKey ( "JwtSecurityTokenUsedToCallWebAPI" ) )
89
103
{
90
- // TODO: The following comment makes no sense !
91
- // Remove the key as Microsoft.Identity.Web library utilizes this key.
104
+ // Removes 'JwtSecurityTokenUsedToCallWebAPI' from Items collection.
92
105
// If not removed then it can cause failure to the application.
106
+ // Because this key is also added by StoreTokenUsedToCallWebAPI method of Microsoft.Identity.Web.
93
107
context . HttpContext . Items . Remove ( "JwtSecurityTokenUsedToCallWebAPI" ) ;
94
108
}
95
109
}
@@ -100,9 +114,9 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
100
114
/// </summary>
101
115
/// <param name="context"></param>
102
116
/// <param name="identity"></param>
103
- private static void RemoveExistingClaim ( ClaimsIdentity identity )
117
+ private static void RemoveExistingClaims ( ClaimsIdentity identity )
104
118
{
105
- // clear an existing claim
119
+ //clear existing claim
106
120
List < Claim > existingGroupsClaims = identity . Claims . Where ( x => x . Type == "groups" ) . ToList ( ) ;
107
121
if ( existingGroupsClaims ? . Count > 0 )
108
122
{
0 commit comments