由于没有“Access-Control-Allow-Origin”标头,使用 Open Id 的 Blazor WebAssembly 身份验证失败

问题描述 投票:0回答:1

我有一个准系统,独立的 Blazor WebAssembly 项目,我通过 Visual Studio 中的模板生成,选项为:.Net Core 7,身份验证:个人帐户,并且没有选中任何复选框(例如“Asp.Net Core Hosted,不要使用顶级语句等),但我无法获得与我的身份提供者一起使用的身份验证。

我所做的就是从上面的模板创建这个项目,并在我的 OAuth 客户端的配置中添加到 wwwroot/appsettings.json。

  "Local": {
    "Authority": <myauthorityurl>,
    "ClientId": <myclientid>,
    "ResponseType": "code",
    "RedirectUri": "https://localhost:7202/authentication/login-callback"
  }

我还确保将 RedirectUri 添加到 oauth 配置设置(在 oauth 端)作为 https://localhost:7202/authentication/login-callback

当我构建并运行项目时,身份验证失败,我在控制台中看到此消息:

Access to XMLHttpRequest at 'https://login.microsoftonline.com/.well-known/openid-configuration' from origin 'https://localhost:7202' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 

visual studio创建的模板遵循这个指南到一个T:https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-authentication-library?view=aspnetcore -7.0&tabs=visual-studio 但是里面没有提到 CORS。

提前谢谢你。我知道在没有代码共享的情况下执行此操作可能有点困难,但我确保按原样生成模板并填写我的配置设置。

asp.net-core authentication openid blazor-webassembly
1个回答
0
投票

我找到了这个 repo,它在我这边运行良好。然后我按照官方文档创建一个新文档,添加

AllowedHosts
,它的项目运行良好。更多详情,请查看下方。

你的

"AllowedHosts": "*"
里面应该有
appsettings.json
。如果你的项目还在开发阶段,你还需要将它添加到
appsettings.Development.json
文件中。

我重现问题并修复它。

{
  "Local": {
    "Authority": "https://accounts.google.com/",
    "ClientId": "103****s.apps.googleusercontent.com",
    "PostLogoutRedirectUri": "https://localhost:44370/authentication/logout-callback",
    "RedirectUri": "https://localhost:44370/authentication/login-callback",
    "ResponseType": "id_token"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}
© www.soinside.com 2019 - 2024. All rights reserved.