Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisualStudioCredential falls through to next chained cred when no account is found #48469

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
- `VisualStudioCredential` will now correctly fall through to the next credential in the chain when no account is found by Visual Studio. ([#48464](https://github.com/Azure/azure-sdk-for-net/issues/48464))

### Other Changes
- Marked `UsernamePasswordCredential` as obsolete because Resource Owner Password Credentials (ROPC) token grant flow is incompatible with multifactor authentication (MFA), which Microsoft Entra ID requires for all tenants. See https://aka.ms/azsdk/identity/mfa for details about MFA enforcement and migration guidance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class VisualStudioCredential : TokenCredential
private static readonly string TokenProviderFilePath = Path.Combine(".IdentityService", "AzureServiceAuth", "tokenprovider.json");
private const string ResourceArgumentName = "--resource";
private const string TenantArgumentName = "--tenant";
private string[] UnavailableErrorStrings => new[] { "TS005" };

private readonly CredentialPipeline _pipeline;
internal string TenantId { get; }
Expand Down Expand Up @@ -119,7 +120,16 @@ private async ValueTask<AccessToken> GetTokenImplAsync(TokenRequestContext reque
}
catch (Exception e)
{
throw scope.FailWrapAndThrow(e, isCredentialUnavailable: _isChainedCredential);
bool containsUnavailableError = false;
foreach (string errorString in UnavailableErrorStrings)
{
if (e.Message.Contains(errorString))
{
containsUnavailableError = true;
break;
}
}
throw scope.FailWrapAndThrow(e, isCredentialUnavailable: _isChainedCredential || containsUnavailableError);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,18 @@ public void OperationCanceledException_throws_CredentialUnavailableException_Whe
var fileSystem = CredentialTestHelpers.CreateFileSystemForVisualStudio();
var credential = InstrumentClient(new VisualStudioCredential(default, default, fileSystem, new TestProcessService(testProcess), new VisualStudioCredentialOptions { IsChainedCredential = true }));

Assert.ThrowsAsync<CredentialUnavailableException>(
async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/" }), CancellationToken.None));
}

[Test]
[TestCase("TS003: Error, TS005: No accounts found. Please go to Tools->Options->Azure Services Authentication, and add an account to be authenticated to Azure services during development.")]
public void GeneralExceptions_With_CertainErrors_throws_CredentialUnavailableException(string exceptionMessage)
{
var testProcess = new TestProcess() { ExceptionOnStartHandler = p => throw new InvalidOperationException(exceptionMessage) };
var fileSystem = CredentialTestHelpers.CreateFileSystemForVisualStudio();
var credential = InstrumentClient(new VisualStudioCredential(default, default, fileSystem, new TestProcessService(testProcess), new VisualStudioCredentialOptions { IsChainedCredential = false }));

Assert.ThrowsAsync<CredentialUnavailableException>(
async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/" }), CancellationToken.None));
}
Expand Down