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/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md
index f5cb080df1a7..ff1c97a5d24e 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]
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
* Supported interrupting login by hitting CTRL+C
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]
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);
}