Skip to content
Open
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 @@ -35,29 +35,27 @@ public async Task<Uri> ListenToSingleRequestAndRespondAsync(
cancellationToken.ThrowIfCancellationRequested();

HttpListener httpListener = null;
string urlToListenTo = string.Empty;
UriBuilder urlToListenTo = new UriBuilder()
{
Scheme = Uri.UriSchemeHttp,
Host = "localhost",
Port = port,
Path = path
};
try
{
if(string.IsNullOrEmpty(path))
{
path = "/";
}
else
{
path = (path.StartsWith("/") ? path : "/" + path);
}

urlToListenTo = "http://localhost:" + port + path;

if (!urlToListenTo.EndsWith("/"))

if (!urlToListenTo.Path.EndsWith("/"))
{
urlToListenTo += "/";
urlToListenTo.Path += "/";
}

httpListener = new HttpListener();
httpListener.Prefixes.Add(urlToListenTo);
httpListener.Prefixes.Add(urlToListenTo.Uri.ToString());
httpListener.Prefixes.Add(new UriBuilder(urlToListenTo.Uri) { Host = "127.0.0.1" }.Uri.ToString());
httpListener.Prefixes.Add(new UriBuilder(urlToListenTo.Uri) { Host = "[::1]" }.Uri.ToString());

TestBeforeStart?.Invoke(urlToListenTo);
TestBeforeStart?.Invoke(urlToListenTo.Uri.ToString());

httpListener.Start();
_logger.Info(() => "Listening for authorization code on " + urlToListenTo);
Expand Down Expand Up @@ -92,7 +90,7 @@ public async Task<Uri> ListenToSingleRequestAndRespondAsync(
if (ex is HttpListenerException)
{
throw new MsalClientException(MsalError.HttpListenerError,
$"An HttpListenerException occurred while listening on {urlToListenTo} for the system browser to complete the login. " +
$"An HttpListenerException occurred while listening on {urlToListenTo.Uri} for the system browser to complete the login. " +
"Possible cause and mitigation: the app is unable to listen on the specified URL; " +
"run 'netsh http add iplisten 127.0.0.1' from the Admin command prompt.",
ex);
Expand Down