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;
}
}
}