@@ -20,7 +20,7 @@ internal class MsAADTool : IMsAADTool
2020 private ProvisioningToolOptions ProvisioningToolOptions { get ; set ; }
2121 private string CommandName { get ; }
2222 public GraphServiceClient GraphServiceClient { get ; set ; }
23- public IAzureManagementAuthenticationProvider AzureManagementAPI { get ; set ; }
23+ public IAzureManagementAuthenticationProvider AzureManagementAPI { get ; set ; }
2424 private MsalTokenCredential TokenCredential { get ; set ; }
2525
2626 public MsAADTool ( string commandName , ProvisioningToolOptions provisioningToolOptions )
@@ -35,10 +35,10 @@ public MsAADTool(string commandName, ProvisioningToolOptions provisioningToolOpt
3535
3636 public async Task < ApplicationParameters ? > Run ( )
3737 {
38- string outputJsonString = string . Empty ;
38+ string outputJsonString = string . Empty ;
3939 if ( TokenCredential != null && GraphServiceClient != null )
4040 {
41- switch ( CommandName )
41+ switch ( CommandName )
4242 {
4343 //--list-aad-apps
4444 case Commands . LIST_AAD_APPS_COMMAND :
@@ -63,14 +63,14 @@ public MsAADTool(string commandName, ProvisioningToolOptions provisioningToolOpt
6363 return null ;
6464 }
6565
66- private async Task < List < DirectoryObject > > GetGraphObjects ( )
66+ private async Task < List < DirectoryObject > ? > GetGraphObjects ( )
6767 {
6868 List < DirectoryObject > graphObjectsList = new List < DirectoryObject > ( ) ;
6969 try
7070 {
7171 var graphObjects = await GraphServiceClient . Me . OwnedObjects
72- . Request ( )
73- . GetAsync ( ) ;
72+ . Request ( )
73+ . GetAsync ( ) ;
7474
7575 if ( graphObjects != null )
7676 {
@@ -96,13 +96,15 @@ private async Task<List<DirectoryObject>> GetGraphObjects()
9696 {
9797 nextPage = null ;
9898 ConsoleLogger . LogMessage ( Resources . FailedToRetrieveADObjectsError , LogMessageType . Error ) ;
99+ return null ;
99100 }
100101 }
101102 }
102103 }
103104 catch ( ServiceException )
104105 {
105106 ConsoleLogger . LogMessage ( Resources . FailedToRetrieveADObjectsError , LogMessageType . Error ) ;
107+ return null ;
106108 }
107109
108110 return graphObjectsList ;
@@ -112,46 +114,48 @@ internal async Task<string> PrintApplicationsList()
112114 {
113115 string outputJsonString = string . Empty ;
114116 var graphObjectsList = await GetGraphObjects ( ) ;
117+ if ( graphObjectsList is null )
118+ {
119+ return new JsonResponse ( CommandName , State . Fail , Resources . FailedToRetrieveADObjectsError ) . ToJsonString ( ) ;
120+ }
121+
115122 IList < Application > applicationList = new List < Application > ( ) ;
116- if ( graphObjectsList != null && graphObjectsList . Any ( ) )
123+ foreach ( var graphObj in graphObjectsList )
117124 {
118- foreach ( var graphObj in graphObjectsList )
125+ if ( graphObj is Application app )
119126 {
120- if ( graphObj is Application app )
121- {
122- applicationList . Add ( app ) ;
123- }
127+ applicationList . Add ( app ) ;
124128 }
129+ }
125130
126- if ( applicationList . Any ( ) )
131+ if ( applicationList . Any ( ) )
132+ {
133+ Organization ? tenant = await GetTenant ( GraphServiceClient ) ;
134+ if ( tenant != null && tenant . TenantType . Equals ( "AAD B2C" , StringComparison . OrdinalIgnoreCase ) )
127135 {
128- Organization ? tenant = await GetTenant ( GraphServiceClient ) ;
129- if ( tenant != null && tenant . TenantType . Equals ( "AAD B2C" , StringComparison . OrdinalIgnoreCase ) )
136+ foreach ( Application app in applicationList )
130137 {
131- foreach ( Application app in applicationList )
132- {
133- app . AdditionalData . Add ( "IsB2C" , true ) ;
134- }
138+ app . AdditionalData . Add ( "IsB2C" , true ) ;
135139 }
140+ }
136141
137- //order list by created date.
138- applicationList = applicationList . OrderByDescending ( app => app . CreatedDateTime ) . ToList ( ) ;
142+ //order list by created date.
143+ applicationList = applicationList . OrderByDescending ( app => app . CreatedDateTime ) . ToList ( ) ;
139144
140- if ( ProvisioningToolOptions . Json )
141- {
142- JsonResponse jsonResponse = new JsonResponse ( CommandName , State . Success , applicationList ) ;
143- outputJsonString = jsonResponse . ToJsonString ( ) ;
144- }
145- else
145+ if ( ProvisioningToolOptions . Json )
146+ {
147+ JsonResponse jsonResponse = new JsonResponse ( CommandName , State . Success , applicationList ) ;
148+ outputJsonString = jsonResponse . ToJsonString ( ) ;
149+ }
150+ else
151+ {
152+ Console . Write (
153+ "--------------------------------------------------------------\n " +
154+ "Application Name\t \t \t \t Application ID\n " +
155+ "--------------------------------------------------------------\n \n " ) ;
156+ foreach ( var app in applicationList )
146157 {
147- Console . Write (
148- "--------------------------------------------------------------\n " +
149- "Application Name\t \t \t \t Application ID\n " +
150- "--------------------------------------------------------------\n \n " ) ;
151- foreach ( var app in applicationList )
152- {
153- Console . WriteLine ( $ "{ app . DisplayName . PadRight ( 35 ) } \t \t { app . AppId } ") ;
154- }
158+ Console . WriteLine ( $ "{ app . DisplayName . PadRight ( 35 ) } \t \t { app . AppId } ") ;
155159 }
156160 }
157161 }
@@ -161,7 +165,7 @@ internal async Task<string> PrintApplicationsList()
161165
162166 private static async Task < Organization ? > GetTenant ( GraphServiceClient graphServiceClient )
163167 {
164- Organization ? tenant = null ;
168+ Organization ? tenant ;
165169 try
166170 {
167171 tenant = ( await graphServiceClient . Organization
@@ -170,23 +174,25 @@ internal async Task<string> PrintApplicationsList()
170174 }
171175 catch ( ServiceException ex )
172176 {
177+ string ? errorMessage ;
173178 if ( ex . InnerException != null )
174179 {
175- Console . WriteLine ( ex . InnerException . Message ) ;
180+ errorMessage = ex . InnerException . Message ;
176181 }
177182 else
178183 {
179184 if ( ex . Message . Contains ( "User was not found" ) || ex . Message . Contains ( "not found in tenant" ) )
180185 {
181- Console . WriteLine ( "User was not found.\n Use both --tenant-id <tenant> --username <username@tenant>.\n And re-run the tool." ) ;
186+ errorMessage = "User was not found.\n Use both --tenant-id <tenant> --username <username@tenant>.\n And re-run the tool." ;
182187 }
183188 else
184189 {
185- Console . WriteLine ( ex . Message ) ;
190+ errorMessage = ex . Message ;
186191 }
187192 }
188193
189- Environment . Exit ( 1 ) ;
194+ Console . WriteLine ( errorMessage ) ;
195+ return null ;
190196 }
191197
192198 return tenant ;
@@ -275,13 +281,13 @@ internal async Task<string> PrintTenantsList()
275281 JsonResponse jsonResponse = new JsonResponse ( CommandName , State . Success , tenantList ) ;
276282 outputJsonString = jsonResponse . ToJsonString ( ) ;
277283 }
278- else
284+ else
279285 {
280286 Console . Write (
281- "--------------------------------------------------------------------------------------------------------------------------------\n " +
287+ "--------------------------------------------------------------------------------------------------------------------------------\n " +
282288 "Display Name\t \t \t Default Domain\t \t \t \t Tenant Type\t Tenant Id\n " +
283289 "--------------------------------------------------------------------------------------------------------------------------------\n \n " ) ;
284- foreach ( var tenant in tenantList )
290+ foreach ( var tenant in tenantList )
285291 {
286292 Console . WriteLine ( $ "{ ( tenant . DisplayName ?? string . Empty ) . PadRight ( 16 ) } \t \t { ( tenant . DefaultDomain ?? string . Empty ) . PadRight ( 20 ) } \t \t { ( tenant . TenantType ?? string . Empty ) . PadRight ( 10 ) } \t { ( tenant . TenantId ?? string . Empty ) } ") ;
287293 }
0 commit comments