diff --git a/sdk/identity/Azure.Identity/CHANGELOG.md b/sdk/identity/Azure.Identity/CHANGELOG.md index 3403f52ea786..2a7dbd016cfb 100644 --- a/sdk/identity/Azure.Identity/CHANGELOG.md +++ b/sdk/identity/Azure.Identity/CHANGELOG.md @@ -7,6 +7,7 @@ ### Breaking Changes ### Bugs Fixed + - Fixed an issue where setting `DefaultAzureCredentialOptions.TenantId` twice throws an `InvalidOperationException` ([#47035](https://github.com/Azure/azure-sdk-for-net/issues/47035)) ### Other Changes diff --git a/sdk/identity/Azure.Identity/src/Credentials/DefaultAzureCredentialOptions.cs b/sdk/identity/Azure.Identity/src/Credentials/DefaultAzureCredentialOptions.cs index 713652498af2..95b362722a35 100644 --- a/sdk/identity/Azure.Identity/src/Credentials/DefaultAzureCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/Credentials/DefaultAzureCredentialOptions.cs @@ -53,22 +53,22 @@ public string TenantId get => _tenantId.Value; set { - if (_interactiveBrowserTenantId.Updated && value != _interactiveBrowserTenantId.Value) + if (_interactiveBrowserTenantId.Updated && _tenantId.Value != _interactiveBrowserTenantId.Value) { throw new InvalidOperationException("Applications should not set both TenantId and InteractiveBrowserTenantId. TenantId is preferred, and is functionally equivalent. InteractiveBrowserTenantId exists only to provide backwards compatibility."); } - if (_sharedTokenCacheTenantId.Updated && value != _sharedTokenCacheTenantId.Value) + if (_sharedTokenCacheTenantId.Updated && _tenantId.Value != _sharedTokenCacheTenantId.Value) { throw new InvalidOperationException("Applications should not set both TenantId and SharedTokenCacheTenantId. TenantId is preferred, and is functionally equivalent. SharedTokenCacheTenantId exists only to provide backwards compatibility."); } - if (_visualStudioTenantId.Updated && value != _visualStudioTenantId.Value) + if (_visualStudioTenantId.Updated && _tenantId.Value != _visualStudioTenantId.Value) { throw new InvalidOperationException("Applications should not set both TenantId and VisualStudioTenantId. TenantId is preferred, and is functionally equivalent. VisualStudioTenantId exists only to provide backwards compatibility."); } - if (_visualStudioCodeTenantId.Updated && value != _visualStudioCodeTenantId.Value) + if (_visualStudioCodeTenantId.Updated && _tenantId.Value != _visualStudioCodeTenantId.Value) { throw new InvalidOperationException("Applications should not set both TenantId and VisualStudioCodeTenantId. TenantId is preferred, and is functionally equivalent. VisualStudioCodeTenantId exists only to provide backwards compatibility."); } diff --git a/sdk/identity/Azure.Identity/tests/DefaultAzureCredentialOptionsTests.cs b/sdk/identity/Azure.Identity/tests/DefaultAzureCredentialOptionsTests.cs index e66d4fbdf0f4..e0d26ea0237d 100644 --- a/sdk/identity/Azure.Identity/tests/DefaultAzureCredentialOptionsTests.cs +++ b/sdk/identity/Azure.Identity/tests/DefaultAzureCredentialOptionsTests.cs @@ -90,7 +90,26 @@ public void ValidateAzureAdditionallyAllowedTenantsEnvVarDefaultHonored(string e } [Test] - public void ValidateShallowCloneCopiesAllProperties([Values]bool useTenantId) + public void DeprecatedTenantIdPropertiesCannotMismatch() + { + var options = new DefaultAzureCredentialOptions(); + options.TenantId = "1"; + Assert.Throws(() => options.InteractiveBrowserTenantId = "2"); + Assert.Throws(() => options.SharedTokenCacheTenantId = "2"); + Assert.Throws(() => options.VisualStudioTenantId = "2"); + Assert.Throws(() => options.VisualStudioCodeTenantId = "2"); + } + + [Test] + public void TenantIdCanBeSetTwice() + { + var options = new DefaultAzureCredentialOptions(); + options.TenantId = "1"; + options.TenantId = "2"; + } + + [Test] + public void ValidateShallowCloneCopiesAllProperties([Values] bool useTenantId) { Random rand = new Random();