window.close()
逻辑的特定页面。但是,我在令牌采集过程中遇到以下例外:
Exception 1:
MsalClientException: An HttpListenerException occurred while listening on http://localhost:5267/ 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.
Exception 2:
HttpListenerException: The process cannot access the file because it is being used by another process.
在我的代码中,我首先尝试使用
AcquireTokenSilent
获取令牌。但是,它总是失败,并且该过程恢复到
AcquireTokenInteractive
。上述例外情况发生在这次后备期间。我当前的代码是:
string[] scopes = new string[] { "user.read" };
var tempUri = "https://login.microsoftonline.com/" + tenantId;
var app = PublicClientApplicationBuilder.Create(appId)
.WithAuthority(new Uri(tempUri))
.WithRedirectUri("http://localhost:5267/ezSearch/Main/Test")
.Build();
AuthenticationResult result;
string token = string.Empty;
try
{
var accounts = app.GetAccountsAsync().Result;
result = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync().Result;
}
catch (AggregateException ex)
{
if (ex.InnerException is MsalUiRequiredException)
{
result = app.AcquireTokenInteractive(scopes)
.WithPrompt(Microsoft.Identity.Client.Prompt.NoPrompt)
.ExecuteAsync().Result; // Exceptions occur here
}
else
{
throw;
}
}
token = result.AccessToken;
问题: 使用
HttpListenerException
时,如何解决AcquireTokenInteractive
?i尝试了“ netsh http add iplisten 127.0.0.1”,然后在Azure应用程序身份验证页面中添加redirecturl 任何见解或建议都将不胜感激。谢谢!
在我的情况下,根本原因是我在Azure应用中注册了多个类似的重定向URI。当我删除除https:// localhost:5267以外的所有不必要的重定向uris时,问题已解决。如果您面对相同的httplistenerexception,我建议检查您的Azure Ad应用程序注册并确保仅配置正确的重定向URI。 希望这有帮助!