【问题标题】:Custom authorisation of an Azure FunctionAzure 函数的自定义授权
【发布时间】:2017-09-14 01:23:48
【问题描述】:
【问题讨论】:
标签:
azure
azure-functions
【解决方案1】:
目前没有对细粒度授权的内置支持。这将为Functions UserVoice 提供很好的建议。
您始终可以将授权逻辑编写为函数的一部分,尽管内置功能肯定会更好。下面的代码 sn-p (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 端点和提供程序图端点的调用结合使用。