【问题标题】:IdentityServer4 authorization error not matching redirect URIIdentityServer4 授权错误与重定向 URI 不匹配
【发布时间】:2020-10-15 04:36:35
【问题描述】:

我有一个托管在 ASP.NET CORE 服务器中的 Blazor WebAssembly 应用程序。我在进程自包含部署中使用 IIS,并且应用程序被配置为默认网站的子应用程序。所以 URL 类似于 [https:]//myserver/APPLICATIONNAME/

我收到一条错误消息,指出重定向 URI 与客户端允许的 URI 不匹配,但在日志中这两个 URI 完全相同。重定向 URI 在 appsettings.json 文件中定义。

我已关注微软文档here

这是没有显式重定向 URI 的日志错误:

{
  "ClientId": "TestBlazorAuth.Client",
  "ClientName": "TestBlazorAuth.Client",
  "AllowedRedirectUris": [
    "/authentication/login-callback"
  ],
  "SubjectId": "anonymous",
  "RequestedScopes": "",
  "Raw": {
    "client_id": "TestBlazorAuth.Client",
    "redirect_uri": "https://localhost/TestBlazorAuth/authentication/login-callback",
    "response_type": "code",
    "scope": "TestBlazorAuth.ServerAPI openid profile",
    "state": "d53f88ebd6dc413fb929f01b06cd8efa",
    "code_challenge": "XPaEMOg02714PWrx9POC3oSwsO2mXAhBe_IerH4p75E",
    "code_challenge_method": "S256",
    "prompt": "none",
    "response_mode": "query"
  }
}

这是显式设置:

"IdentityServer": {
    "Clients": {
      "TestBlazorAuth.Client": {
        "Profile": "IdentityServerSPA",
        "RedirectUri": "https://localhost/TestBlazorAuth/authentication/login-callback"
      }
    },
    "Key": {
      "Type": "File",
      "FilePath": "C:\\temp\\som_cert_file.pfx",
      "Password": "blablabla"
    }
  },

这是使用显式设置时的错误日志:

Invalid redirect_uri: https://localhost/TestBlazorAuth/authentication/login-callback
{
  "ClientId": "TestBlazorAuth.Client",
  "ClientName": "TestBlazorAuth.Client",
  "AllowedRedirectUris": [
    "https://localhost/TestBlazorAuth/authentication/login-callback"
  ],
  "SubjectId": "anonymous",
  "RequestedScopes": "",
  "Raw": {
    "client_id": "TestBlazorAuth.Client",
    "redirect_uri": "https://localhost/TestBlazorAuth/authentication/login-callback",
    "response_type": "code",
    "scope": "TestBlazorAuth.ServerAPI openid profile",
    "state": "3b121b72d20640cb8c7b74f783b5d914",
    "code_challenge": "WNev6kwUV7UjAhNTvvDH10vJwkEXPDev8jwNZYnsNDY",
    "code_challenge_method": "S256",
    "prompt": "none",
    "response_mode": "query"
  }
}

redirect_uri 和 AllowedRedirectUris[0] 在我看来完全一样。

这是使用 NLOG 的跟踪:

2020-06-24 14:56:05.3948||TRACE|IdentityServer4.Stores.ValidatingClientStore|Calling into client configuration validator: IdentityServer4.Validation.DefaultClientConfigurationValidator 
2020-06-24 14:56:05.4080||DEBUG|IdentityServer4.Stores.ValidatingClientStore|client configuration validation for client Integra.PCSP.Print.WebUI.Client succeeded. 
2020-06-24 14:56:05.4080||ERROR|IdentityServer4.Validation.AuthorizeRequestValidator|Invalid redirect_uri: "https://localhost/TestBlazorAuth/authentication/login-callback"

我查看了 IdentityServer4 源代码以检查比较是如何完成的,一切似乎都很好:

    public class StrictRedirectUriValidator : IRedirectUriValidator
    {
        /// <summary>
        /// Checks if a given URI string is in a collection of strings (using ordinal ignore case comparison)
        /// </summary>
        /// <param name="uris">The uris.</param>
        /// <param name="requestedUri">The requested URI.</param>
        /// <returns></returns>
        protected bool StringCollectionContainsString(IEnumerable<string> uris, string requestedUri)
        {
            if (uris.IsNullOrEmpty()) return false;

            return uris.Contains(requestedUri, StringComparer.OrdinalIgnoreCase);
        }

    ...
    }

此时我已准备好开始项目并在 MVC 中实现应用程序,它只有几页,所以我认为这将是一个很棒的 Blazor 概念证明。事实证明,开发部分很容易,部署是一场噩梦。

感谢您的任何想法。

【问题讨论】:

  • 抱歉这个愚蠢的问题,但是这个网址只能通过浏览器或邮递员访问吗?
  • 是的,它可以访问并返回 200 代码和一些 HTML 代码。
  • 所以这与 blazor 无关,这完全是 IS4 的问题。您可以尝试“SPA”配置文件而不是“IdentityServerSPA”吗?
  • 我认为这个链接应该是与localhost和scheme的绝对链接: "AllowedRedirectUris": [ "/authentication/login-callback"

标签: asp.net iis identityserver4 blazor webassembly


【解决方案1】:

问题出在区分大小写上。不过我还是不明白为什么。

这就是我所做的。

  1. 更新 Blazor wasm 解决方案 wwwroot/index.html(URI 全部小写!)
<base href="/testblazorauth/" />   
  1. 更新服务器项目 Startup.cs(URI 全部小写!)
app.UsePathBase("/testblazorauth/");
  1. 更新 appsettings.json(删除显式 RedirectUri 属性)。框架将使用第二步中的值并正确构建回调 URI。
"IdentityServer": {
    "Clients": {
      "TestBlazorAuth.Client": {
        "Profile": "IdentityServerSPA"        
      }
    },
    "Key": {
      "Type": "File",
      "FilePath": "C:\\temp\\SOME_CERT.pfx",
      "Password": "SOME_PASSWORD"
    }
  },

在 IIS 中,我创建了新的默认网站子应用程序,其主机名为“testblazorauth”(全部小写)。

使用 VS 发布应用并在 Chrome 中运行,得到了预期的结果,没有更多错误。

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2017-11-28
    • 2015-06-27
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多