回复地址与请求授权时提供的回复地址不匹配。

3
我是一位有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我正在处理一个与 .netcore3.1 应用相关的错误,这让我感到沮丧。

以下是我的设置:

在 Startup.cs 中:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftWebApp(options =>
    {
        Configuration.Bind("AzureAd", options);
        options.Events ??= new OpenIdConnectEvents();
        options.Events.OnRedirectToIdentityProvider = async ctx =>
        {
            ctx.ProtocolMessage.RedirectUri = Configuration["AzureAd:RedirectUri"];
            await Task.CompletedTask;
        };
    })
    .AddMicrosoftWebAppCallsWebApi(Configuration, new[] { "user.read" })
    .AddDistributedTokenCaches();

在我的应用程序设置中

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "ClientId": "ClientId",
    "TenantId": "TenantId",
    "RedirectUri": "https://mywebsite.com/signin-oidc",
    "ClientSecret": "ClientSecret"
  },

Azure应用程序注册已正确配置,将https://mywebsite.com/signin-oidc作为重定向URL。我还添加了以下内容,以允许从代理转发标头:
services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownNetworks.Clear();
    options.KnownProxies.Clear();
});

// then in the Configure method

app.UseForwardedHeaders();

但我仍然遇到了这个困扰我的错误。我认为通过添加

ctx.ProtocolMessage.RedirectUri = Configuration["AzureAd:RedirectUri"];

我会克服这个问题,但似乎对此错误没有影响。
An unhandled exception occurred","Properties":{"CorrelationId":"c3393560-6ebe-41ce-99fc-693c1a387474","Path":"/signin-oidc","Method":"POST","exceptionMessage":"An error was encountered while handling the remote login.","exception":"System.Exception: An error was encountered while handling the remote login.\n ---> MSAL.NetCore.4.16.1.0.MsalServiceException: \n\tErrorCode: invalid_client\nMicrosoft.Identity.Client.MsalServiceException: A configuration issue is preventing authentication - check the error message from the server for details.You can modify the configuration in the application registration portal. See https://aka.ms/msal-net-invalid-client for details.  Original exception: AADSTS500112: The reply address 'http://mywebsite.com/signin-oidc' does not match the reply address 'https://mywebsite.com/signin-oidc' provided when requesting Authorization

请检查服务器的错误消息以获取详细信息。你可以查看服务器日志文件吗? - undefined
2个回答

2
你的代理是否也终止了 HTTPS 连接,导致你的应用程序只收到了 HTTP 流量?

是的,但是 app.UseForwardedHeaders(); 这部分应该能解决这个问题吧? - undefined
从错误消息中,我可以看到应用程序将重定向URL设置为https://mywebsite.com/signin-oidc,但是您的应用程序配置的重定向URI是http://mywebsite.com/signin-oidc。不同之处在于HTTPS和HTTP。 - undefined
这是问题所在。尽管我已经使用了app.UseForwardedHeaders();,但代理服务器没有配置转发任何内容,而我却以为它已经配置好了,这让我感到困惑。我们更新了代理服务器以转发头部信息,并添加了 HTTPS 重定向,这完全解决了这个问题。 - undefined
你能具体说明一下你是如何解决这个问题的吗?我们在 Kubernetes 上运行一个简单的 ASP.NET 应用程序,遇到了完全相同的情况。如果我们的 ASP.NET 容器以 80 端口模式运行,这种方式永远不会起作用吗?我们让 ingress 在其上设置 SSL。 - undefined

1
根据您的错误信息:回复地址与请求授权时提供的回复地址不匹配。必须确保Azure门户中配置的redirect_uri与代码中配置的redirect_uri完全相同。
当您访问应用程序网址时,将被重定向到登录页面。解码授权请求URL,您将找到redirect_uri,复制redirect_uri的值并粘贴到Azure门户中,然后再次尝试。

enter image description here

对于重定向URL,它应该以https开头,如果您需要以http开头,则必须将其配置为http://localhost


我已经检查过了,授权URL的redirect_uri参数是https地址,这是正确的。所以为什么我会收到这个消息,真是让人困惑! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接