【问题标题】:Custom authorisation of an Azure FunctionAzure 函数的自定义授权
【发布时间】:2017-09-14 01:23:48
【问题描述】:

我已经设置了我的 Azure 函数,我可以看到有支持 Azure Active Directory 进行身份验证的选项,这看起来很棒。在之前的项目中,我使用 .NET Core 托管 WebAPI,随后使用授权策略 (https://docs.microsoft.com/en-us/aspnet/core/security/authorization/) 在我的 API 中提供基于声明的细粒度授权。我似乎无法在 Azure 函数中找到等效机制。

谁能告诉我是否有办法在 Azure 函数中做这种事情?

【问题讨论】:

    标签: 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 端点和提供程序图端点的调用结合使用。

    【讨论】:

    • 谢谢,我会把它作为建议发布在提供的链接上。
    猜你喜欢
    • 2023-02-06
    • 1970-01-01
    • 2021-01-04
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多