asp.net - 带有 ASP.Net Core Identity 登录链接的 Blazor WebAssembly 站点中出现网络错误

标签 asp.net identityserver4 blazor blazor-webassembly

在我的 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/代码可以在https://github.com/jonasarcangel/BlazorLoginNetworkErrorIssue中找到

最佳答案

现在很明显,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 确定。

如果您在浏览器地址栏中输入网址http://167.172.118.170/.well-known/openid-configuration,您将看到有关身份提供商的所有配置信息。确实,http://167.172.118.170/才是权威。但正如您所见,在 appsettings.json 文件中将权限设置为该 url 会被忽略,而会使用内部 url。

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

建议的尝试:

在Web服务器项目的Startup类的ConfigureService中更改以下代码:

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

services.AddIdentityServer(options =>
            {
                options.IssuerUri = "https://167.172.118.170/";
               
            })
              .AddApiAuthorization<ApplicationUser, 
                                ApplicationIdentityDbContext>(); 
  • Use the ForwardedHeaders middlewareSee this sample至于如何做。

  • 坚持上述...问题就在这里,而不是其他地方。

  • 祝你好运...

    关于asp.net - 带有 ASP.Net Core Identity 登录链接的 Blazor WebAssembly 站点中出现网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64201620/

    相关文章:

    c# - 跟踪生产中的 asp.net 网站

    c# - 如何同时运行 ASP.NET MVC 和 WebAPI 项目?

    asp.net - 如何访问 ASP.net 中嵌套中继器内的按钮?

    c# - 签名验证失败。无法匹配 'kid'

    c# - IdentityServer4 错误,此客户端不允许离线访问

    c# - 存储过程在 SSMS 中运行良好,但在 C# 中调用时没有返回任何行

    javascript - 如何在 MVC 应用程序中使用 JWT,其中使用 IdentityServer4 的某些 javascript 部分

    c# - Blazor 问题 "getting started"

    c# - 如果变量更改发生在单独的线程上,Blazor 不会检测到变量更改

    c# - 开拓者 : how to bind Nullable object to <select>?