Skip to content

Commit e8fd3c6

Browse files
authored
Erich/fix write object error (#13466)
* fix Write-xxx in worker thread * add change log
1 parent a8ae7fd commit e8fd3c6

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public override void ExecuteCmdlet()
404404
subscriptionName,
405405
password,
406406
SkipValidation,
407-
WriteWarning,
407+
WriteWarningEvent, //Could not use WriteWarning directly because it may be in worker thread
408408
name,
409409
shouldPopulateContextList,
410410
MaxContextPopulation));
@@ -419,7 +419,7 @@ public override void ExecuteCmdlet()
419419

420420
try
421421
{
422-
var result = (PSAzureProfile)(task.ConfigureAwait(false).GetAwaiter().GetResult());
422+
var result = (PSAzureProfile)task.Result;
423423
WriteObject(result);
424424
}
425425
catch (AuthenticationFailedException ex)

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419]
2122
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
2223
* Supported interrupting login by hitting <kbd>CTRL</kbd>+<kbd>C</kbd>
2324
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]

src/Accounts/Authentication/Factories/AuthenticationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public IAccessToken Authenticate(
123123
{
124124
while (processAuthenticator != null && processAuthenticator.TryAuthenticate(GetAuthenticationParameters(tokenCacheProvider, account, environment, tenant, password, promptBehavior, promptAction, tokenCache, resourceId), out authToken))
125125
{
126-
token = authToken?.ConfigureAwait(true).GetAwaiter().GetResult();
126+
token = authToken?.ConfigureAwait(false).GetAwaiter().GetResult();
127127
if (token != null)
128128
{
129129
// token.UserId is null when getting tenant token in ADFS environment
@@ -142,7 +142,7 @@ public IAccessToken Authenticate(
142142
{
143143
if (!IsTransientException(e) || retries == 0)
144144
{
145-
throw e;
145+
throw;
146146
}
147147

148148
TracingAdapter.Information(string.Format("[AuthenticationFactory] Exception caught when calling TryAuthenticate, retrying authentication - Exception message: '{0}'", e.Message));

src/Accounts/Authenticators/DeviceCodeAuthenticator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
using Microsoft.Azure.Commands.Common.Authentication;
2323
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
24+
using Microsoft.Azure.Commands.ResourceManager.Common;
2425

2526
namespace Microsoft.Azure.PowerShell.Authenticators
2627
{
@@ -74,7 +75,7 @@ public override bool CanAuthenticate(AuthenticationParameters parameters)
7475
private void WriteWarning(string message)
7576
{
7677
EventHandler<StreamEventArgs> writeWarningEvent;
77-
if (AzureSession.Instance.TryGetComponent("WriteWarning", out writeWarningEvent))
78+
if (AzureSession.Instance.TryGetComponent(AzureRMCmdlet.WriteWarningKey, out writeWarningEvent))
7879
{
7980
writeWarningEvent(this, new StreamEventArgs() { Message = message });
8081
}

src/Accounts/Authenticators/MsalAccessToken.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static async Task<IAccessToken> GetAccessTokenAsync(
7373
string userId = null,
7474
string homeAccountId = "")
7575
{
76-
var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken);
76+
var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false);
7777
return new MsalAccessToken(tokenCredential, requestContext, token.Token, token.ExpiresOn, tenantId, userId, homeAccountId);
7878
}
7979

@@ -84,9 +84,9 @@ public static async Task<IAccessToken> GetAccessTokenAsync(
8484
TokenRequestContext requestContext,
8585
CancellationToken cancellationToken)
8686
{
87-
var record = await authTask;
87+
var record = await authTask.ConfigureAwait(false);
8888
cancellationToken.ThrowIfCancellationRequested();
89-
var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken);
89+
var token = await tokenCredential.GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false);
9090

9191
return new MsalAccessToken(tokenCredential, requestContext, token.Token, token.ExpiresOn, record.TenantId, record.Username, record.HomeAccountId);
9292
}

0 commit comments

Comments
 (0)