azure - 具有 Microsoft 身份平台身份验证的 Web API

标签 azure azure-active-directory azure-web-app-service openid-connect .net-6.0

我基于 VS2022 模板创建了一个新的 ASP.NET Core Web API,其身份验证类型为“Microsoft 身份平台”。

在 Azure 上,我使用试用订阅设置了以下内容(我是全局管理员):

  • 创建应用 API
  • 创建应用注册

为了仔细检查应用程序是否正在运行,我还添加了一个 TestController,它返回一个简单的字符串。

设置 azure 后,我相应地更改了 appsettings.json 文件:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "[xxx]",
    "TenantId": "[xxx]",
    "ClientId": "[xxx]",
    "Scopes": "access_as_user",
    "CallbackPath": "/signin-oidc"
  },

其他一切都已经在program.cs中设置好了(我只为您提取了相关代码):

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

之后,我在 Azure Devops 上设置了 YAML 管道,以将 API 直接部署到云。

部署似乎有效:我可以访问 TestController 的功能。但是,当我尝试访问天气预报时,我收到状态代码(我使用全局管理员用户尝试此操作):

401 Unauthorized

到目前为止我尝试过什么?

  • 我将订阅和应用服务本身(即应用 Api)的用户添加到访问控制 (IAM) 贡献者(用于测试)

请注意,模板的 WeatherForecastController 如下所示:

[ApiController]
[Route("[controller]")]
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

对于我在这里缺少什么或者我可以在 Azure 上检查什么来缩小问题范围有什么想法吗?

最佳答案

请检查您是否缺少带有 [Authorize] 的 Controller 属性。 要保护 ASP.NET 或 ASP.NET Core Web API,您必须将 [Authorize] 属性添加到以下项目之一:

1.The controller itself if you want all controller actions to be protected.

  1. The individual controller action for your API
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    [RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
    public class WeatherForecastController : ControllerBase
    {

登录后,在浏览器中查看https://jwt.io中收到的token并对其进行解码并查看发行者(“iss”声明)中存在哪个端点,即 v1 或 v2,然后转到 azure 广告门户并更改 accesstokenacceptedversion如果它不符合该端点,即;对于 V1,它必须为 null 或 12 for V2 .

enter image description here

如果正确,请在门户中为该 api 公开所需的范围。

enter image description here

并确保提供所需的 API 权限并获得管理员同意。

enter image description here

  • 确保在身份验证中选中标记 ID token 和访问 token Blade 。
  • 当你生成一个 token 时,它的范围应该是这样的 api://clientid_of_the_app_exposed_api/access_as_user哪个行 与代码中的配置匹配。

尝试上述操作后,如果仍然出现错误,请检查audience是否=客户端id值,如果不是,请在appsettings中添加前缀api://更改clientId在客户端 ID 之前。即 clientId= api://<clientId>

引用文献:

  1. Protected web API
  2. Protected web API: Verify scopes and app roles

关于azure - 具有 Microsoft 身份平台身份验证的 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72142643/

相关文章:

azure - 通过 VS Code 编辑 Azure 逻辑应用标准的代码

Azure ARM 模板 - 参数与属性的静态值相结合

Azure AD list - 可选声明

azure - 在 Microsoft Identity 平台上以编程方式撤销 Oauth2 访问权限

Azure 网站应用程序见解 - 切换配置

azure - Power BI 访问 blob 存储,文件头不存在

azure - 使用 azure oauth 客户端凭据流时如何添加可选声明以访问 token

python - 如何从存储在 Azure 文件共享上的大型 NetCDF 文件中提取数据并将其发送到 Azure 网页

c# - 从azure网站访问证书时 key 集不存在

Azure EventGrid 触发器 - Webhooks