@@ -24,32 +24,45 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
2424 // Checks if the incoming token contained a 'Group Overage' claim.
2525 if ( context . Principal . Claims . Any ( x => x . Type == "hasgroups" || ( x . Type == "_claim_names" && x . Value == "{\" groups\" :\" src1\" }" ) ) )
2626 {
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 > ( ) ;
2929
30- if ( graph == null )
30+ if ( graphClient == null )
3131 {
3232 Console . WriteLine ( "No service for type 'Microsoft.Graph.GraphServiceClient' has been registered in the Startup." ) ;
3333 }
34+
35+ // Checks if the SecurityToken is not null.
36+ // For the Web App, SecurityToken contains value of the ID Token.
3437 else if ( context . SecurityToken != null )
3538 {
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.
3741 if ( ! context . HttpContext . Items . ContainsKey ( "JwtSecurityTokenUsedToCallWebAPI" ) )
3842 {
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.
4046 context . HttpContext . Items . Add ( "JwtSecurityTokenUsedToCallWebAPI" , context . SecurityToken as JwtSecurityToken ) ;
4147 }
4248
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 .
4450 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+ }
5063 if ( memberPage ? . Count > 0 )
5164 {
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 .
5366 var allgroups = ProcessIGraphServiceMemberOfCollectionPage ( memberPage ) ;
5467
5568 if ( allgroups ? . Count > 0 )
@@ -85,11 +98,12 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
8598 }
8699 finally
87100 {
101+ // Checks if the key 'JwtSecurityTokenUsedToCallWebAPI' exists.
88102 if ( context . HttpContext . Items . ContainsKey ( "JwtSecurityTokenUsedToCallWebAPI" ) )
89103 {
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.
92105 // 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.
93107 context . HttpContext . Items . Remove ( "JwtSecurityTokenUsedToCallWebAPI" ) ;
94108 }
95109 }
@@ -100,9 +114,9 @@ public static async Task ProcessClaimsForGroupsOverage(TokenValidatedContext con
100114 /// </summary>
101115 /// <param name="context"></param>
102116 /// <param name="identity"></param>
103- private static void RemoveExistingClaim ( ClaimsIdentity identity )
117+ private static void RemoveExistingClaims ( ClaimsIdentity identity )
104118 {
105- // clear an existing claim
119+ //clear existing claim
106120 List < Claim > existingGroupsClaims = identity . Claims . Where ( x => x . Type == "groups" ) . ToList ( ) ;
107121 if ( existingGroupsClaims ? . Count > 0 )
108122 {
0 commit comments