blazor - Fresh Blazor Webassembly 模板 CLI v5.0.100 生成 "Unauthorized"

标签 blazor blazor-webassembly

抱歉这个新问题。
我真的需要了解这个新模板的解决方法是什么。

重现错误:
Visual Studio,全新的 Blazor 应用程序,名为“Demo3”

.NET 5.0,Blazor WebAssembly 应用程序,模板“CLI v5.0.100”
ASP.NET Core 托管,https
身份验证、单域、读取目录数据

运行创建

我已经在以前的模板上做了数百次。但是这个"new"模板立即产生了一个我无法弄清楚的错误。

详情:
正常登录
点击“获取数据”(天气预报)
我收到此错误消息。

关键字:401 和未授权。
我猜Unauthorized,是重要的信息。

info: System.Net.Http.HttpClient.Demo3.ServerAPI.ClientHandler[100]
      Sending HTTP request GET https://localhost:44345/WeatherForecast
WeatherForecast:1 Failed to load resource: the server responded with a status of 401 ()
blazor.webassembly.js:1 info: System.Net.Http.HttpClient.Demo3.ServerAPI.ClientHandler[101]
      Received HTTP response after 1272.7899ms - Unauthorized
blazor.webassembly.js:1 info: System.Net.Http.HttpClient.Demo3.ServerAPI.LogicalHandler[101]
      End processing HTTP request after 1340.08ms - Unauthorized
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Response status code does not indicate success: 401 (Unauthorized).
System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
  at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () <0x337bf70 + 0x00052> in <filename unknown>:0 
  at System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsyncCore[T] (System.Threading.Tasks.Task`1[TResult] taskResponse, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x32c6740 + 0x000f4> in <filename unknown>:0 
  at Demo3.Client.Pages.FetchData.OnInitializedAsync () [0x0003c] in D:\repos\nameofrepo\src\Demo3\Demo3\Client\Pages\FetchData.razor:48 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2c764b0 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2f401a8 + 0x000b6> in <filename unknown>:0 

如错误所示,存在授权错误。

我的问题是:
有人找到此错误的“官方 MS 链接”了吗?
或者,谁能解释一下哪里出了问题。

跟进
只是为了清楚起见,我没有更改或添加任何代码。模板完成后直接进行测试运行。当然,我曾尝试调试错误并查看 AzureAD 中的设置,但没有成功。

第二次跟进
调查了一个提示,评论中的 AADSTS7000218 细节。

所以我建立了一个项目。以下是详细信息:
我换成了 Kestrel。
我将 Azure App 设置更改为 SPA,具有相同的重定向地址。

为自己授予这两个应用程序的所有者权限。
将服务器添加到客户端应用程序的 api 权限。 “api://BlazorApp1_Server/user_impersonation”

测试:同样的错误。 细节,我仍然注意到观众细节。

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Response status code does not indicate success: 401 (Unauthorized).
System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).

在响应头中:

www-authenticate: Bearer error="invalid_token", error_description="The audience 'api://blazorapp1_server' is invalid"

但没有关于 AADSTS7000218 或缺少客户端密码的信息。

仍然没有放弃......我会回来的......

最佳答案

我通过将“Audience”值添加到服务器的 appsettings.json 文件中设法解决了我项目中的这个错误

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<domain>",
    "TenantId": "<guid>",
    "ClientId": "api://<apiname>",
    "CallbackPath": "/signin-oidc",
    "Audience": "api://blazorapp1_server"
  },

如果 ClientId 包含大写字母并且不记名 token 中传递的受众值全部为小写,则受众验证似乎区分大小写并且失败。添加所有小写的受众值(value)为我解决了这个问题。

关于blazor - Fresh Blazor Webassembly 模板 CLI v5.0.100 生成 "Unauthorized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65001011/

相关文章:

blazor - 如何在服务器端 Blazor 中使用 HttpContext 对象来检索有关用户、用户代理的信息

Blazor wasm - 调试 - 无法查看任何变量的值

c# - 使用 IAsyncEnumerable 通过 Blazor 通过 HTTP 流式传输文本行

blazor - 为 Blazor WebAssembly 项目创建声明

c# - 如何在 Blazor WebAssembly-PWA 中将本地数据同步到服务器?

Blazor - 如何防止文本框 (InputText) 中出现非数字字符?

c# - 如何使用环境变量覆盖 Blazor 客户端配置?

c# - Blazor:命名空间 'ApplicationPartAttributeAttribute' 中不存在类型或命名空间名称 'Microsoft.AspNetCore.Mvc.ApplicationParts'

blazor - 使用 Blazor 单击提交后清除 InputFields

c# - 如何像 Vue 一样从父层向 Blazor 组件随机添加 CSS 属性?