如何使用Azure Functions强制执行A AD应用程序角色授权?

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

This page描述如何使用清单将Application应用角色添加到Azure Active Directory中的应用程序。

页面中的代码示例:

"appId": "8763f1c4-f988-489c-a51e-158e9ef97d6a",
"appRoles": [
    {
      "allowedMemberTypes": [
        "Application"
      ],
      "displayName": "ConsumerApps",
      "id": "47fbb575-859a-4941-89c9-0f7a6c30beac",
      "isEnabled": true,
      "description": "Consumer apps have access to the consumer data.",
      "value": "Consumer"
    }
  ],
"availableToOtherTenants": false,

当从使用client_credentials授予类型进行身份验证的应用程序中调用Azure函数时,如何强制其属于应用程序角色?

我已经用Google搜索,但是找不到清晰的文档来说明如何对Azure Functions进行此授权。


我的测试功能应用

我已经从邮递员调用的Azure门户中创建了一个简单的“ hello ” Azure函数。

#r "Microsoft.Azure.WebJobs.Extensions.Http"
#r "Newtonsoft.Json"

using System.Net;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;


public static IActionResult Run(HttpRequest req,  ILogger log, ClaimsPrincipal claimsPrincipal)
{
    var name = req.Query["name"];
    log.LogInformation($"C# HTTP trigger function processed a request: {name}");

    var cp = new {
        Identity = new {
            claimsPrincipal.Identity.AuthenticationType,
            claimsPrincipal.Identity.IsAuthenticated,
            claimsPrincipal.Identity.Name
        },
        Claims = claimsPrincipal.Claims.Select(claim => new
        {
            claim.Type,
            claim.Value
        })
    };
    log.LogInformation($"ClaimsPrincipal ({claimsPrincipal.GetType().FullName}): {JsonConvert.SerializeObject(cp, Formatting.Indented)}");

    return (IActionResult)new OkObjectResult($"Hello, {name}");
}

首先,我使用https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/token进行身份验证并捕获access_token

请求正文示例:

grant_type:client_credentials
client_id:<Application ID>
client_secret:<Client Secret>
scope:https://<Function-app-name>.azurewebsites.net/.default

示例结果:

{
    "token_type": "Bearer",
    "expires_in": 3599,
    "ext_expires_in": 3599,
    "access_token": "eyJ0eXAi......"
}

然后我使用https://<function-app-name>.azurewebsites.net/api/hello?name=World和包含Authorization: Bearer eyJ0eXAi......的标头调用我的Azure函数。>

身份验证可以正常工作,就像调用Azure函数一样。但是,我可以通过应用程序注册

在Azure门户中添加新的应用程序,进行身份验证,然后自由调用Azure函数。我不知道如何将对Azure函数的访问限制为仅具有特定应用程序角色的应用程序。

此页面描述了如何使用清单将应用程序应用角色添加到Azure Active Directory中的应用程序。页面中的代码示例:“ appId”:“ 8763f1c4-f988-489c-a51e-158e9ef97d6a”,“ ...

oauth-2.0 azure-active-directory authorization azure-functions
1个回答
0
投票

我不知道如何将对Azure功能的访问限制为仅具有特定应用程序角色的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.