Azure功能的自定义授权
问题描述:
我已经设置了我的Azure功能,我可以看到有支持Azure Active Directory进行身份验证的选项,这看起来很棒。在之前的项目中,我使用.NET Core来托管WebAPI,随后使用授权策略(https://docs.microsoft.com/en-us/aspnet/core/security/authorization/)在我的API中提供细粒度的基于声明的授权。我似乎无法在Azure功能中找到等效的机制。Azure功能的自定义授权
任何人都可以告诉我,如果有办法在Azure功能中做这种事情吗?
答
目前还没有内置的细粒度授权支持。这将是Functions UserVoice的一个很好的建议项目。
尽管内置功能肯定会更好,但您始终可以将授权逻辑编写为您的功能的一部分。以下代码片段(C#)在代码中执行身份验证检查并打印声明列表。您可以修改它以要求特定的声明:
using System.Net;
using System.Threading;
using System.Security.Claims;
public static void Run(HttpRequestMessage req, TraceWriter log)
{
if (!Thread.CurrentPrincipal.Identity.IsAuthenticated)
{
log.Info("Not authenticated");
return req.CreateResponse(HttpStatusCode.Unauthorized);
}
ClaimsIdentity identity = (Thread.CurrentPrincipal as ClaimsPrincipal)?.Identity as ClaimsIdentity;
if (identity != null)
{
foreach (var claim in identity.Claims)
{
log.Info($"{claim.Type} = {claim.Value}");
}
}
// Rest of your function...
return req.CreateResponse(HttpStatusCode.OK);
}
请注意,在非.NET语言中,您需要检查声明信息头。您也可以将其与调用/.auth/me端点和提供程序图端点的调用结合使用。
谢谢,我会在提供的链接中将其作为建议发布。 – Slicc