An HttpListenerException occurred while listening on http://localhost:7271/ for the system browser to complete the login. Possible cause and mitigation: the app is unable to listen on the specified URL;

Upulatheja Dodamthalawa/LKA 0 Reputation points
2024-09-02T09:06:37.09+00:00

I'm calling microsoft graph API beta version API to create an approval item. It expect ApprovalSolution.ReadWrite delegate permission. Frist i need to authenticate the user using delegate permission method. I'm using MS windows 365 sandbox environment to set an entra instance with teams and azure application.

in the application -> Authentication, i have added a web platform and set the redirect uri. and then i have created an mvc web application with latest microsoft identity client and graph beta version. below is the code to authenticate the user. but it gives error below. I have tried many possibilities but still couldn't fix this issue.

"An HttpListenerException occurred while listening on http://localhost:5145/ 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."

public static async Task<AuthenticationResult> AuthenticateInteractivelyAsync()

{

_app = PublicClientApplicationBuilder

.Create(ClientId)

.WithAuthority("https://login.microsoftonline.com/<Tenant Id>")

.WithRedirectUri("https://localhost:7271/") // This can be any valid URI for public client apps

.Build();

var scopes = new string[]

{

"https://graph.microsoft.com/User.Read",

"https://graph.microsoft.com/Group.Read.All"

};

try

{

var accounts = await _app.GetAccountsAsync();

AuthenticationResult result = await _app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())

.ExecuteAsync();

Console.WriteLine($"Access Token: {result.AccessToken}");

return result;

}

catch (MsalUiRequiredException)

{

try

{

var result = await _app.AcquireTokenInteractive(scopes)

.ExecuteAsync();

Console.WriteLine($"Access Token: {result.AccessToken}");

return result;

}

catch (MsalException msalex)

{

Console.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");

return null;

}

}

}

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
687 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,888 questions
Microsoft Entra
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 24,481 Reputation points Microsoft Employee
    2024-09-06T19:54:15.83+00:00

    Hi @Upulatheja Dodamthalawa/LKA , as the error states, make sure that the app can listen on the specified URL. Run the following command from an Admin command prompt to add the IP address to the list of IP addresses that the HTTP server listens to:

    netsh http add iplisten 127.0.0.1
    

    Let me know your results after doing this.

    Best,

    James

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.