【问题标题】:Reply address does not match the reply address provided when requesting authorization回复地址与请求授权时提供的回复地址不符
【发布时间】:2020-12-03 20:53:36
【问题描述】:

我正在处理的 .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"
  },

使用https://mywebsite.com/signin-oidc 作为重定向 url 正确配置了 Azure 应用注册。 我还添加了以下内容以允许从代理转发标头:

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

【问题讨论】:

  • check the error message from the server for details... 你能检查一下服务器日志文件吗

标签: c# .net-core azure-active-directory openid-connect identity


【解决方案1】:

会不会是您的代理也终止了 HTTPS,所以您的应用程序获得的流量是 HTTP?

【讨论】:

  • 是但不应该 app.UseForwardedHeaders();部分修复?
  • 从错误消息中,我可以看到应用程序将重定向 url 传递为https://mywebsite.com/signin-oidc,但您的应用程序配置了重定向 uri 作为http://mywebsite.com/signin-oidc。不同的是 HTTPS 和 HTTP。
  • 这就是问题所在。即使我有app.UseForwardedHeaders();,代理也没有配置为转发任何东西,我的印象是它让我感到困惑。我们更新了代理以转发标头并添加了 Https 重定向,这完全解决了这个问题
  • 您能否更具体地说明您为解决问题所做的工作?对于在 Kubernetes 中运行的简单 ASP.NET 应用程序,我们正在经历完全相同的事情。如果我们的 ASP.NET 容器在端口 80 模式下运行,这将永远无法工作吗?我们让我们的入口将 SSL 设置在顶部。
【解决方案2】:

根据您的错误提示:回复地址与请求授权时提供的回复地址不匹配。必须确保 Azure 门户中配置的 redirect_uri 与代码中配置的 redirect_uri 完全相同。

当您访问应用程序 url 时,您将被重定向到登录页面。解码授权请求URL,你会发现redirect_uri,复制redirect_uri的值并粘贴到Azure Portal,然后重试。

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

【讨论】:

  • 我已经检查过了,授权 url 有 https 地址作为 redirect_uri 参数,这是正确的。这就是为什么我会收到这条消息如此令人困惑的原因!
猜你喜欢
  • 2021-12-02
  • 2020-01-24
  • 1970-01-01
  • 2018-10-30
  • 2018-06-22
  • 1970-01-01
  • 2016-06-26
  • 2015-06-21
  • 1970-01-01
相关资源
最近更新 更多