diff --git a/src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs b/src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs index 22ddd37ec3..12b4ae23d0 100644 --- a/src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs +++ b/src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs @@ -118,9 +118,13 @@ private Uri ComputeHttpEndpoint(Uri authority, RequestContext requestContext) authority.Host : AadAuthority.DefaultTrustedHost; - string instanceDiscoveryEndpoint = UriBuilderExtensions.GetHttpsUriWithOptionalPort( - $"https://{discoveryHost}/common/discovery/instance", - authority.Port); + string instanceDiscoveryEndpoint = $"https://{discoveryHost}/common/discovery/instance"; + if (string.Equals(discoveryHost, authority.Host, StringComparison.OrdinalIgnoreCase)) + { + instanceDiscoveryEndpoint = UriBuilderExtensions.GetHttpsUriWithOptionalPort( + instanceDiscoveryEndpoint, + authority.Port); + } requestContext.Logger.InfoPii( () => $"Fetching instance discovery from the network from host {discoveryHost}. Endpoint {instanceDiscoveryEndpoint}. ", diff --git a/src/client/Microsoft.Identity.Lab.Api/Http/MockHttpManagerExtensions.cs b/src/client/Microsoft.Identity.Lab.Api/Http/MockHttpManagerExtensions.cs index 98a4438320..a4d5123301 100644 --- a/src/client/Microsoft.Identity.Lab.Api/Http/MockHttpManagerExtensions.cs +++ b/src/client/Microsoft.Identity.Lab.Api/Http/MockHttpManagerExtensions.cs @@ -40,7 +40,11 @@ public static MockHttpMessageHandler AddInstanceDiscoveryMockHandler( ? authorityURI.Host : AadAuthority.DefaultTrustedHost; - discoveryEndpoint = UriBuilderExtensions.GetHttpsUriWithOptionalPort($"https://{discoveryHost}/common/discovery/instance", authorityURI.Port); + discoveryEndpoint = $"https://{discoveryHost}/common/discovery/instance"; + if (string.Equals(discoveryHost, authorityURI.Host, StringComparison.OrdinalIgnoreCase)) + { + discoveryEndpoint = UriBuilderExtensions.GetHttpsUriWithOptionalPort(discoveryEndpoint, authorityURI.Port); + } } else { diff --git a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/AadAuthorityTests.cs b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/AadAuthorityTests.cs index 8b7186044d..b65c09ba16 100644 --- a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/AadAuthorityTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/AadAuthorityTests.cs @@ -297,12 +297,14 @@ public void TenantlessAuthorityChanges() [TestMethod] //Test for bug #1292 (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/1292) - public void AuthorityCustomPortTest() + public void AuthorityCustomPortIsPreservedAndNotUsedForGlobalInstanceDiscoveryTest() { const string customPortAuthority = "https://localhost:5215/common/"; using var harness = CreateTestHarness(); - harness.HttpManager.AddInstanceDiscoveryMockHandler(customPortAuthority); + harness.HttpManager.AddInstanceDiscoveryMockHandler( + customPortAuthority, + new Uri("https://login.microsoftonline.com/common/discovery/instance")); PublicClientApplication app = PublicClientApplicationBuilder.Create(TestConstants.ClientId) .WithAuthority(new Uri(customPortAuthority), false) diff --git a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/InstanceDiscoveryManagerTests.cs b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/InstanceDiscoveryManagerTests.cs index 485ab0f5b5..65d3d8b3ef 100644 --- a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/InstanceDiscoveryManagerTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/InstanceDiscoveryManagerTests.cs @@ -91,6 +91,51 @@ await ValidateSelfEntryAsync(new Uri(TestConstants.ADFSAuthority)) .ConfigureAwait(false); } + [TestMethod] + public async Task NetworkMetadataProvider_PreservesCustomPort_WhenDiscoveryHostMatchesAuthority_Async() + { + // Arrange + const string authority = "https://login.microsoftonline.com:5215/common/"; + const string expectedDiscoveryEndpoint = "https://login.microsoftonline.com:5215/common/discovery/instance"; + var networkMetadataProvider = new NetworkMetadataProvider( + _harness.HttpManager, + _networkCacheMetadataProvider); + var handler = _harness.HttpManager.AddInstanceDiscoveryMockHandler( + authority, + new Uri(expectedDiscoveryEndpoint)); + + // Act + await networkMetadataProvider.GetMetadataAsync(new Uri(authority), _testRequestContext) + .ConfigureAwait(false); + + // Assert + Assert.IsNotNull(handler.ActualRequestMessage); + Assert.AreEqual(5215, handler.ActualRequestMessage.RequestUri.Port); + Assert.AreEqual(expectedDiscoveryEndpoint, handler.ActualRequestMessage.RequestUri.GetLeftPart(UriPartial.Path)); + } + + [TestMethod] + public async Task NetworkMetadataProvider_DropsCustomPort_WhenDiscoveryEndpointIsNotSetAndAuthorityIsUnknown_Async() + { + // Arrange + const string authority = "https://localhost:5215/common/"; + const string expectedDiscoveryEndpoint = "https://login.microsoftonline.com/common/discovery/instance"; + var networkMetadataProvider = new NetworkMetadataProvider( + _harness.HttpManager, + _networkCacheMetadataProvider); + var handler = _harness.HttpManager.AddInstanceDiscoveryMockHandler(authority); + + // Act + await networkMetadataProvider.GetMetadataAsync(new Uri(authority), _testRequestContext) + .ConfigureAwait(false); + + // Assert + Assert.IsNotNull(handler.ActualRequestMessage); + Assert.AreEqual("login.microsoftonline.com", handler.ActualRequestMessage.RequestUri.Host); + Assert.AreEqual(443, handler.ActualRequestMessage.RequestUri.Port); + Assert.AreEqual(expectedDiscoveryEndpoint, handler.ActualRequestMessage.RequestUri.GetLeftPart(UriPartial.Path)); + } + [TestMethod] public async Task NetworkCacheProvider_IsUsedFirst_Async() {