c# - Azure 示例 4-1-MyOrg 引发 "HttpRequestException: Invalid status code in the HttpResponseMessage: Unauthorized."错误

标签 c# azure .net-core

发现此示例中很少有直接针对此问题的解决方案。要么该示例相对较新或已更新,并且没有人尝试过,要么我显然做了一些不正确的事情。使用最新的 VSCode 在 Windows 10 中运行此程序。我是 Azure 新手(最近获得了基础知识认证),对 .Net 世界也有些陌生。两者都可能对我不利。

我已按照找到的说明进行操作 here多次但得到相同的结果。完整的堆栈跟踪 FWIW 是

TodoListClient.Services.TodoListService.GetAsync() in TodoListService.cs

                throw new HttpRequestException($"Invalid status code in the HttpResponseMessage: {response.StatusCode}.");

TodoListClient.Controllers.TodoListController.Index() in TodoListController.cs

                return View(await _todoListService.GetAsync());

lambda_method(Closure , object )
Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable+Awaiter.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ExceptionContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我知道它在提示什么,我只是不知道去哪里解决它。我不确定,但我怀疑它与注册应用程序时的范围设置指令有关:

开始步骤:

选择“添加范围”按钮,打开“添加范围”屏幕,然后输入如下所示的值:

对于范围名称,请使用 access_as_user。

选择“谁可以同意?”的“管理员和用户”选项。

对于管理员同意显示名称,请输入 Access TodoListService-aspnetcore-webapi。

对于管理员同意描述类型,允许应用以登录用户身份访问 TodoListService-aspnetcore-webapi。

对于用户同意显示名称,输入 Access TodoListService-aspnetcore-webapi。

对于用户同意描述,输入允许应用程序代表您访问 TodoListService-aspnetcore-webapi。

保持状态为启用。

选择底部的“添加范围”按钮以保存此范围。

结束步骤

我无法做的就是遵循“为谁可以同意?选择管理员和用户选项”。步骤,因为没有选择“管理员和用户”的选项,只能选择“管理员”,这是这里的问题吗?我已多次执行这些步骤,但无法解决该错误。当我登录时,我看到初始网页,但当我单击“TodoList”链接时,我收到“未经授权”错误。

我在我创建的新租户中创建了一个新用户,但无论是它还是我的全局管理员用户都无法访问“Todolist”应用程序。

我错过了什么?有人有这个工作吗?

最佳答案

我下载了示例代码并让它在我这边运行,这是我的 Azure AD 应用程序配置:

对于服务 Azure AD 应用程序配置:

应用 ID:a6b73b06-450a-4fac-a7bb-569c3644594c

公开 API: enter image description here

对于客户端 Azure AD 应用程序配置:

应用 ID:d2a53db5-da38-47b7-97f1-2d27a9dd056d

身份验证配置: enter image description here

API 所需权限: enter image description here

创建客户端 key : enter image description here

服务项目applications.json设置:

enter image description here

客户端项目applications.json设置: enter image description here

结果: enter image description here

关于c# - Azure 示例 4-1-MyOrg 引发 "HttpRequestException: Invalid status code in the HttpResponseMessage: Unauthorized."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67067500/

相关文章:

c# - ViewModel 应该如何引用它的 Models 属性?

c# - 使用多个任务从大型集合中检索所有记录

azure - 创建 Azure 管道时出现 ResourcePurchaseValidationFailed 错误

azure |表单识别器预选文件?

c# - 等概率范围内的随机数

c# - 选中 form2 上的复选框时如何更改 Form1 label.text?

azure - 将 Azure Data Lake Storage 挂载到 Windows VM 文件系统

c# - 无法将自定义 ActiveX 控件添加到 .NET Core WinForms 应用程序,但 .NET Framework 应用程序工作正常

linux - .NET Core 的 MongoDB C# 驱动程序中不受支持的位置运算符的解决方法

c# - .NET Core 中的 wsHttpBinding 解决方法