blazor - Fresh Blazor Webassembly Template CLI v5.0.100 产生“未经授权”
问题描述
对不起,新手的问题。
我真的需要了解这个新模板的解决方法是什么。
重现错误:
Visual Studio,全新的 Blazor App,名为“Demo3”
.NET 5.0,Blazor WebAssembly 应用程序,模板“CLI v5.0.100”
托管 ASP.NET Core,https
身份验证,单域,读取目录数据
运行创建
我已经在以前的模板上这样做了数百次。但是这个“新”模板立即产生了一个我无法弄清楚的错误。
详细信息:
正常登录
单击“获取数据”(天气预报)
我收到此错误消息。
关键字:401 和未经授权。
我猜未授权是重要信息。
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 包含大写字母并且在承载令牌中传递的受众值全部为小写,则验证失败。添加所有小写的观众价值为我解决了这个问题。
推荐阅读
- java - InterruptedIOException 是否将线程的 Interrupted 标志设置为 true?
- php - db 获取和查看文件的日志消息
- angular - Angular 和 ng 选择。选定项目的确认
- ios - UIScreenEdgePanGestureRecognizer
- javascript - JS/jQuery 承诺
- python - json 转换器到 csv , simplejson.errors.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
- php - 使用基本身份验证工作的 PHP SOAP 请求
- powershell - 使用 powershell 生成 SQL 创建表脚本
- node.js - Node.js v11.9.0 上的 ECMAScript 模块
- mysql - 将坐标点沿某个方向移动 X 米