当前位置: 首页 > 知识库问答 >
问题:

如何使用Azure功能强制AAD应用程序角色授权?

西门伟
2023-03-14

此页面描述如何使用清单将应用程序应用程序角色添加到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,

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

我谷歌了一下,但找不到解释如何为Azure Functions进行此授权的明确留档。

我的测试功能应用程序

我创建了一个简单的“你好”

#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/

请求正文示例:

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://调用我的Azure函数

身份验证工作正常,调用Azure Function也是如此。但是,我可以通过Azure门户中的应用程序注册添加新应用程序,进行身份验证,然后自由调用Azure Function。我不知道如何将Azure Function的访问限制为仅具有特定应用程序角色的应用程序。


共有1个答案

柯乐池
2023-03-14

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

如果您只想让拥有ConsumerApps权限的应用程序访问您的功能,请执行以下步骤。

1、在门户网站的Azure Active Directory中导航到您功能的广告应用程序-

2、然后你可以再次尝试使用你的广告应用获取令牌,你会发现该应用无法成功获取令牌,你会得到如下错误,因为你的客户端应用没有ConsumerApps权限。

3.要成功访问该功能,我们只需添加您使用的客户端AD应用程序的应用程序权限。

导航到门户中的客户端广告应用程序-

等待一段时间,然后再次尝试获取令牌,效果很好。

使用令牌调用函数,也可以。

 类似资料:
  • 因此,我一直在尝试生成一个小功能应用程序(JS)来响应发送到Azure IoT中心的消息。输出是一个简单的控制台日志。但是,我无法将IoT集线器的事件集线器兼容endpoint加载为触发器(唯一可用的选项是创建新的事件集线器)。我该如何进行?设备发送到Azure IoT Hub的代码正在运行,我可以通过设备浏览器工具查看消息。

  • Microsoft.Azure.WebJobs.Host:索引方法“Fun Ction1.Run”时出错。Microsoft.Azure.WebJobs.Host:无法将参数“log”绑定到类型TraceWriter。请确保绑定支持参数类型。如果您正在使用绑定扩展(例如ServiceBus、Timers等),请确保您在启动代码(例如config)中调用了扩展的注册方法。

  • Using a delegation key The collaborator can now push to the repository using Docker Content Trust. Docker will automatically choose and pick the right key for the targets/release role. Edit the file o

  • 已将本地Web应用程序(. net 4.6.1)迁移到Azure,该应用程序对其用户使用Windows身份验证,然后使用AD集成身份验证连接到MSSQL数据库。 理想情况下,应该保留现有的安全机制,即向AD用户授予对数据库对象的权限,并让DB成为应用程序的安全源。 已创建并配置SQL数据库的副本。 如果我在本地运行web应用程序,但使用新的Azure数据库,则一切正常。 如果我运行此命令,我将获得

  • 场景:我有一个使用B2C身份验证的Blazor wasm应用程序,需要调用HTTP触发的Azure函数。保护Azure函数的最佳方法是什么,这样只有Blazor应用程序和/或经过身份验证的用户才能调用该函数? 到目前为止,我知道如何用B2C保护Blazor应用程序(显然很傻!)我还能够为Azure函数添加B2C身份验证,并通过验证jwt令牌来保护调用。但我不清楚这两部分该如何组合在一起。

  • 我找不到Nuget包的Windows服务版本。 有人知道它叫什么吗(如果它存在的话)?