Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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}. ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
bgavrilMS marked this conversation as resolved.
Resolved
{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading