为什么我得到一个帐户的 URI 不匹配,而另一个帐户却没有?

Why do I get URI Mismatch for one account and not another?

使用 Azure。我注册的应用程序是 Public 客户端(这是一个桌面应用程序)。

下面的代码在“USERA”的帐户下工作得很好。

但是,运行 与“USERB”相同的代码我得到以下异常

MSAL.NetCore.4.42.0.0.MsalClientException: 错误代码:loopback_response_uri_mismatch Microsoft.Identity.Client.MsalClientException: 重定向 URI 不匹配。预期 (/favicon.ico) 实际 (/)。 在 Microsoft.Identity.Client.Platforms.Shared.Desktop.OsBrowser.DefaultOsBrowserWebUi.AcquireAuthorizationAsync(Uri authorizationUri,Uri redirectUri,RequestContext requestContext,CancellationToken cancellationToken) 在 Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.FetchAuthCodeAndPkceInternalAsync(IWebUI webUi,CancellationToken cancellationToken) 在 Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.FetchAuthCodeAndPkceVerifierAsync(CancellationToken cancellationToken) 在 Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.GetTokenResponseAsync(CancellationToken cancellationToken) 在 Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.ExecuteAsync(CancellationToken cancellationToken) 在 Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)

              pubApp = PublicClientApplicationBuilder.Create(config.ClientId)
                                .WithLogging(Log, LogLevel.Verbose, true)
                                .WithAuthority(config.Authority)
                                .WithRedirectUri("http://localhost:12345/")                               
                                .Build();

   /** 
    * A call to Graph API calls the following
    */
                    authResult = await clientApp.AcquireTokenInteractive(scopes)
                        .WithUseEmbeddedWebView(false)
                        .WithPrompt(Prompt.SelectAccount)
                        .ExecuteAsync();

请检查您在代码中提供的重定向 URI。

If your app pops up a window with no address bar, then it is using the "embedded browser"

对于使用嵌入式浏览器的桌面应用程序,Microsoft 建议使用重定向 URI 作为: https://login.microsoftonline.com/common/oauth2/nativeclient

If your app brings your system's default browser (such as Edge, Chrome, Firefox, etc.) to visit Microsoft login portal, then it is using the "system browser".

对于使用 system 浏览器的桌面应用程序,Microsoft 建议使用重定向 URI 作为: http://localhost

在代码中,您提到了 .WithUseEmbeddedWebView(false),这意味着您没有使用 嵌入式 浏览器。

请检查两个用户帐户是否使用不同的系统浏览器。

尝试将 .WithRedirectUri() 字段中的重定向 URI 更改为类似下面的内容作为 解决方法:

"http://localhost:12345""http://localhost""https://localhost"

如果对您有帮助,请查找以下参考资料。

参考文献: Ref1, Ref2, Ref3