Azure Functions 的自定义授权
Custom authorisation of an Azure Function
我已经设置了我的 Azure Function,我可以看到有支持 Azure Active Directory 进行身份验证的选项,看起来不错。在之前的项目中,我使用 .NET Core 托管 WebAPI,随后使用授权策略 (https://docs.microsoft.com/en-us/aspnet/core/security/authorization/) 在我的 API 中提供基于声明的细粒度授权。我似乎无法在 Azure 函数中找到等效机制。
谁能告诉我是否有办法在 Azure 函数中执行此类操作?
目前 built-in 不支持 fine-grained 授权。这将成为 Functions UserVoice.
的一个很好的建议项目
您始终可以将授权逻辑编写为函数的一部分,尽管 built-in 功能肯定会更好。下面的代码片段 (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 语言中,您需要检查 headers 以获取声明信息。您还可以将其与对 /.auth/me 端点和提供程序图端点的调用结合起来。
我已经设置了我的 Azure Function,我可以看到有支持 Azure Active Directory 进行身份验证的选项,看起来不错。在之前的项目中,我使用 .NET Core 托管 WebAPI,随后使用授权策略 (https://docs.microsoft.com/en-us/aspnet/core/security/authorization/) 在我的 API 中提供基于声明的细粒度授权。我似乎无法在 Azure 函数中找到等效机制。
谁能告诉我是否有办法在 Azure 函数中执行此类操作?
目前 built-in 不支持 fine-grained 授权。这将成为 Functions UserVoice.
的一个很好的建议项目您始终可以将授权逻辑编写为函数的一部分,尽管 built-in 功能肯定会更好。下面的代码片段 (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 语言中,您需要检查 headers 以获取声明信息。您还可以将其与对 /.auth/me 端点和提供程序图端点的调用结合起来。