From 8a1becbec78e63133fb35541a6d92e551eab8bfd Mon Sep 17 00:00:00 2001 From: "Erich(Renyong) Wang" Date: Mon, 9 Nov 2020 10:52:54 +0800 Subject: [PATCH 1/2] fix Write-xxx in worker thread --- src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs | 4 ++-- .../Authentication/Factories/AuthenticationFactory.cs | 4 ++-- src/Accounts/Authenticators/DeviceCodeAuthenticator.cs | 3 ++- src/Accounts/Authenticators/MsalAccessToken.cs | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs index c9340a07ad63..20de1ac242cd 100644 --- a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs +++ b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs @@ -404,7 +404,7 @@ public override void ExecuteCmdlet() subscriptionName, password, SkipValidation, - WriteWarning, + WriteWarningEvent, //Could not use WriteWarning directly because it may be in worker thread name, shouldPopulateContextList, MaxContextPopulation)); @@ -419,7 +419,7 @@ public override void ExecuteCmdlet() try { - var result = (PSAzureProfile)(task.ConfigureAwait(false).GetAwaiter().GetResult()); + var result = (PSAzureProfile)task.Result; WriteObject(result); } catch (AuthenticationFailedException ex) diff --git a/src/Accounts/Authentication/Factories/AuthenticationFactory.cs b/src/Accounts/Authentication/Factories/AuthenticationFactory.cs index e839af671d95..5dc8c058978e 100644 --- a/src/Accounts/Authentication/Factories/AuthenticationFactory.cs +++ b/src/Accounts/Authentication/Factories/AuthenticationFactory.cs @@ -123,7 +123,7 @@ public IAccessToken Authenticate( { while (processAuthenticator != null && processAuthenticator.TryAuthenticate(GetAuthenticationParameters(tokenCacheProvider, account, environment, tenant, password, promptBehavior, promptAction, tokenCache, resourceId), out authToken)) { - token = authToken?.ConfigureAwait(true).GetAwaiter().GetResult(); + token = authToken?.ConfigureAwait(false).GetAwaiter().GetResult(); if (token != null) { // token.UserId is null when getting tenant token in ADFS environment @@ -142,7 +142,7 @@ public IAccessToken Authenticate( { if (!IsTransientException(e) || retries == 0) { - throw e; + throw; } TracingAdapter.Information(string.Format("[AuthenticationFactory] Exception caught when calling TryAuthenticate, retrying authentication - Exception message: '{0}'", e.Message)); diff --git a/src/Accounts/Authenticators/DeviceCodeAuthenticator.cs b/src/Accounts/Authenticators/DeviceCodeAuthenticator.cs index dadd8485687f..4bbd50757350 100644 --- a/src/Accounts/Authenticators/DeviceCodeAuthenticator.cs +++ b/src/Accounts/Authenticators/DeviceCodeAuthenticator.cs @@ -21,6 +21,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.ResourceManager.Common; namespace Microsoft.Azure.PowerShell.Authenticators { @@ -74,7 +75,7 @@ public override bool CanAuthenticate(AuthenticationParameters parameters) private void WriteWarning(string message) { EventHandler writeWarningEvent; - if (AzureSession.Instance.TryGetComponent("WriteWarning", out writeWarningEvent)) + if (AzureSession.Instance.TryGetComponent(AzureRMCmdlet.WriteWarningKey, out writeWarningEvent)) { writeWarningEvent(this, new StreamEventArgs() { Message = message }); } diff --git a/src/Accounts/Authenticators/MsalAccessToken.cs b/src/Accounts/Authenticators/MsalAccessToken.cs index 41c506f718c0..b2480c7ca632 100644 --- a/src/Accounts/Authenticators/MsalAccessToken.cs +++ b/src/Accounts/Authenticators/MsalAccessToken.cs @@ -73,7 +73,7 @@ public static async Task GetAccessTokenAsync( string userId = null, string homeAccountId = "") { - var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken); + var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false); return new MsalAccessToken(tokenCredential, requestContext, token.Token, token.ExpiresOn, tenantId, userId, homeAccountId); } @@ -84,9 +84,9 @@ public static async Task GetAccessTokenAsync( TokenRequestContext requestContext, CancellationToken cancellationToken) { - var record = await authTask; + var record = await authTask.ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); - var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken); + var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false); return new MsalAccessToken(tokenCredential, requestContext, token.Token, token.ExpiresOn, record.TenantId, record.Username, record.HomeAccountId); } From fc8a8f09d6d4e4e3f57a4eb8cee6ea8b2071d5a5 Mon Sep 17 00:00:00 2001 From: "Erich(Renyong) Wang" Date: Mon, 9 Nov 2020 23:30:34 +0800 Subject: [PATCH 2/2] add change log --- src/Accounts/Accounts/ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md index a281b19be8db..9e0a0a63fdfa 100644 --- a/src/Accounts/Accounts/ChangeLog.md +++ b/src/Accounts/Accounts/ChangeLog.md @@ -18,6 +18,7 @@ - Additional information about change #1 --> ## Upcoming Release +* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419] * Supported interrupting login by hitting CTRL+C * Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127] * Fixed null reference and method case insensitive in `Invoke-AzRestMethod`