Gid*_*sey 6 authorization oauth-2.0 azure-active-directory azure-functions
本页介绍如何使用清单将应用程序应用程序角色添加到 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,
Run Code Online (Sandbox Code Playgroud)
当从使用授权类型进行身份验证的应用程序调用 Azure 函数时client_credentials
,如何强制它属于应用程序角色?
我在 Google 上搜索过,但无法找到明确的文档来解释如何为 Azure Functions 完成此授权。
我的测试功能应用程序
我在 Azure 门户中创建了一个简单的“hello <name>”Azure 函数,我从 Postman 调用该函数。
#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}");
}
Run Code Online (Sandbox Code Playgroud)
首先,我使用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
Run Code Online (Sandbox Code Playgroud)
结果示例:
{
"token_type": "Bearer",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": "eyJ0eXAi......"
}
Run Code Online (Sandbox Code Playgroud)
然后我使用https://<function-app-name>.azurewebsites.net/api/hello?name=World
包含Authorization: Bearer eyJ0eXAi......
.
身份验证工作正常,调用 Azure 函数也是如此。但是,我可以通过 Azure 门户中的应用程序注册添加新应用程序,进行身份验证,然后自由调用 Azure 函数。我不知道如何将 Azure 功能的访问限制为仅具有特定应用程序角色的应用程序。
我不知道如何将 Azure 功能的访问限制为仅具有特定应用程序角色的应用程序。
如果您只想让有权限的应用程序ConsumerApps
访问您的功能,请按照以下步骤操作。
1.导航到门户中 Azure Active Directory 中您的功能的 AD 应用 -> 单击Managed application in local directory
-> Properties
-> 将User assignment required
设为Yes
。
2.然后你可以再次尝试使用AD应用程序获取令牌,你会发现应用程序无法成功获取令牌,你会得到如下错误,因为你的客户端应用程序没有权限ConsumerApps
。
3.要成功访问该功能,我们只需为您使用的Client AD App添加应用程序权限即可。
导航到门户中的客户端 AD 应用程序 -> API permissions
-> Add a permission
-> 单击APIs my organization uses
-> 搜索您的函数 AD 应用程序名称 -> 单击应用程序 -> Application permissions
-> 添加Consumer
权限 ->单击Grant admin consent for xxx
按钮。
等待一段时间,然后再次尝试获取令牌,就可以正常工作了。
使用令牌调用函数,也可以。
归档时间: |
|
查看次数: |
1481 次 |
最近记录: |