|
11 | 11 | using System.Threading.Tasks;
|
12 | 12 | using System.Security;
|
13 | 13 | using OfficeDevPnP.Core;
|
| 14 | +using System.Management.Automation; |
| 15 | +using PnP.PowerShell.Commands.Base; |
14 | 16 |
|
15 | 17 | namespace PnP.PowerShell.Commands.Model
|
16 | 18 | {
|
@@ -181,11 +183,28 @@ public static GenericToken AcquireApplicationToken(string tenant, string clientI
|
181 | 183 |
|
182 | 184 | try
|
183 | 185 | {
|
184 |
| - tokenResult = confidentialClientApplication.AcquireTokenSilent(scopes, account.First()).ExecuteAsync().GetAwaiter().GetResult(); |
| 186 | + tokenResult = confidentialClientApplication.AcquireTokenSilent(scopes, account.First()).WithForceRefresh(true).ExecuteAsync().GetAwaiter().GetResult(); |
185 | 187 | }
|
186 | 188 | catch
|
187 | 189 | {
|
188 |
| - tokenResult = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult(); |
| 190 | + try |
| 191 | + { |
| 192 | + tokenResult = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult(); |
| 193 | + } |
| 194 | + catch (MsalUiRequiredException msalEx) |
| 195 | + { |
| 196 | + if (msalEx.Classification == UiRequiredExceptionClassification.ConsentRequired) |
| 197 | + { |
| 198 | + if (clientId == PnPConnection.PnPManagementShellClientId) |
| 199 | + { |
| 200 | + throw new PSInvalidOperationException("Please provide consent to the PnP Management Shell application with 'Register-PnPManagementShellAccess' and follow the steps on screen."); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + throw msalEx; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
189 | 208 | }
|
190 | 209 |
|
191 | 210 | return new GenericToken(tokenResult.AccessToken);
|
@@ -228,7 +247,24 @@ public static GenericToken AcquireApplicationToken(string tenant, string clientI
|
228 | 247 | }
|
229 | 248 | catch
|
230 | 249 | {
|
231 |
| - tokenResult = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult(); |
| 250 | + try |
| 251 | + { |
| 252 | + tokenResult = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult(); |
| 253 | + } |
| 254 | + catch (MsalUiRequiredException msalEx) |
| 255 | + { |
| 256 | + if (msalEx.Classification == UiRequiredExceptionClassification.ConsentRequired) |
| 257 | + { |
| 258 | + if (clientId == PnPConnection.PnPManagementShellClientId) |
| 259 | + { |
| 260 | + throw new PSInvalidOperationException("Please provide consent to the PnP Management Shell application with 'Register-PnPManagementShellAccess' and follow the steps on screen."); |
| 261 | + } |
| 262 | + else |
| 263 | + { |
| 264 | + throw msalEx; |
| 265 | + } |
| 266 | + } |
| 267 | + } |
232 | 268 | }
|
233 | 269 | return new GenericToken(tokenResult.AccessToken);
|
234 | 270 | }
|
@@ -267,7 +303,24 @@ public static GenericToken AcquireApplicationTokenInteractive(string clientId, s
|
267 | 303 | }
|
268 | 304 | catch
|
269 | 305 | {
|
270 |
| - tokenResult = publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync().GetAwaiter().GetResult(); |
| 306 | + try |
| 307 | + { |
| 308 | + tokenResult = publicClientApplication.AcquireTokenInteractive(scopes).WithExtraScopesToConsent(new[] { "https://graph.microsoft.com/.default" }).ExecuteAsync().GetAwaiter().GetResult(); |
| 309 | + } |
| 310 | + catch (MsalUiRequiredException msalEx) |
| 311 | + { |
| 312 | + if (msalEx.Classification == UiRequiredExceptionClassification.ConsentRequired) |
| 313 | + { |
| 314 | + if (clientId == PnPConnection.PnPManagementShellClientId) |
| 315 | + { |
| 316 | + throw new PSInvalidOperationException("Please provide consent to the PnP Management Shell application with 'Register-PnPManagementShellAccess' and follow the steps on screen."); |
| 317 | + } |
| 318 | + else |
| 319 | + { |
| 320 | + throw msalEx; |
| 321 | + } |
| 322 | + } |
| 323 | + } |
271 | 324 | }
|
272 | 325 | return new GenericToken(tokenResult.AccessToken);
|
273 | 326 | }
|
@@ -351,7 +404,24 @@ public static GenericToken AcquireDelegatedTokenWithCredentials(string clientId,
|
351 | 404 | }
|
352 | 405 | catch
|
353 | 406 | {
|
354 |
| - tokenResult = publicClientApplication.AcquireTokenByUsernamePassword(scopes, username, securePassword).ExecuteAsync().GetAwaiter().GetResult(); |
| 407 | + try |
| 408 | + { |
| 409 | + tokenResult = publicClientApplication.AcquireTokenByUsernamePassword(scopes, username, securePassword).ExecuteAsync().GetAwaiter().GetResult(); |
| 410 | + } |
| 411 | + catch (MsalUiRequiredException msalEx) |
| 412 | + { |
| 413 | + if (msalEx.Classification == UiRequiredExceptionClassification.ConsentRequired) |
| 414 | + { |
| 415 | + if (clientId == PnPConnection.PnPManagementShellClientId) |
| 416 | + { |
| 417 | + throw new PSInvalidOperationException("Please provide consent to the PnP Management Shell application with 'Register-PnPManagementShellAccess' and follow the steps on screen."); |
| 418 | + } |
| 419 | + else |
| 420 | + { |
| 421 | + throw msalEx; |
| 422 | + } |
| 423 | + } |
| 424 | + } |
355 | 425 | }
|
356 | 426 |
|
357 | 427 | return new GenericToken(tokenResult.AccessToken);
|
|
0 commit comments