ASP.Net 核心身份登录 link 的 Blazor WebAssembly 站点中的网络错误

Network Error in Blazor WebAssembly site with ASP.Net Core Identity login link

在我的 BlazorWebAssembly + ASP.NET Core Identity 测试站点 (.NET 5.0 RC1) 中,我在尝试登录时遇到以下错误。

There was an error trying to log you in: 'Network Error'

我已经将 appsettings OIDC 设置如下:

{
  "SiteName": "MyProject",
  "oidc": {
    "Authority": "https://167.172.118.170/",
    "ClientId": "MyProject",
    "DefaultScopes": [
      "openid",
      "profile"
    ],
    "PostLogoutRedirectUri": "/",
    "RedirectUri": "https://167.172.118.170/authentication/login-callback",
    "ResponseType": "code"
  }
}

为什么连接不上?

测试地点在http://167.172.118.170/ and the code can be found in https://github.com/jonasarcangel/BlazorLoginNetworkErrorIssue

我刚刚测试 url http://167.172.118.170/_configuration/BlazorWorld.Web.Client returns

{
    "authority": "http://localhost:5008",
    "client_id": "BlazorWorld.Web.Client",
    "redirect_uri": "http://localhost:5008/authentication/login-callback",
    "post_logout_redirect_uri": "http://localhost:5008/authentication/logout-callback",
    "response_type": "code",
    "scope": "BlazorWorld.Web.ServerAPI openid profile"
}

然后应用程序尝试连接到 http://localhost:5008/.well-known/openid-configuration :

所以部署的应用程序设置可能不是很好。

现在很清楚,Blazor 使用内部 url http://localhost:5008 作为权限,而不是外部 url http://167.172.118.170/

当客户端尝试连接到 http://localhost:5008/.well-known/openid-configuration 时,发生错误:连接被拒绝...

事实上,客户端应该使用这个 url: http://167.172.118.170/.well-known/openid-configuration,但它不会,因为权限的值由 Blazor 确定。

如果您在浏览器的地址栏中键入url http://167.172.118.170/.well-known/openid-configuration,您将看到有关身份提供者的所有配置信息。的确,http://167.172.118.170/就是权限。但是正如您所见,在 appsettings.json 文件中将 Authority 设置为此 url 被简单地忽略了,而是使用了内部的 url。

如何解决这个问题?我们应该告诉 Blazor 不要使用内部的 url 而要使用外部的...

建议尝试次数:

在 Web 服务器项目的 Startup class 的 ConfigureService 中更改此代码:

services.AddIdentityServer()
           .AddApiAuthorization<ApplicationUser, ApplicationIdentityDbContext> 
            ();

services.AddIdentityServer(options =>
            {
                options.IssuerUri = "https://167.172.118.170/";
               
            })
              .AddApiAuthorization<ApplicationUser, 
                                ApplicationIdentityDbContext>(); 
  1. Use the ForwardedHeaders middleware. See this sample至于怎么做。

  2. 坚持上面的...问题在这里,而不是其他地方。

祝你好运...