如何在获得MSAL和SAML身份验证的获取性互动期间修复Httplistenerexception? 我正在努力将SAML身份验证与Microsoft Graph API集成以获得OAuth2访问令牌。我的目标是完成身份验证过程,而无需任何其他浏览器窗口

问题描述 投票:0回答:1
“身份验证完成。您可以返回应用程序。”

为了防止这种情况,我将重定向的URI设置为仅包含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。

希望这有帮助!

c# oauth-2.0 microsoft-graph-api saml
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.