【问题标题】:Azure - Execute some code before each request to Function appAzure - 在对函数应用的每个请求之前执行一些代码
【发布时间】:2019-10-17 10:05:32
【问题描述】:

我创建了一个 azure 函数应用并在其中创建了一些函数。 我想检查用户是否有权访问路由(来自我的数据库)。 我需要在请求执行之前检查这一点。 如何在函数应用中实现这一点?

【问题讨论】:

    标签: node.js azure azure-web-app-service azure-functions


    【解决方案1】:

    如果您在 c# 中使用函数 v2,那么您可以编写自己的 Startup class 并将其注册到您的函数中。 Startup class 总是在函数执行之前执行。

    还请确保您使用的是最新版本的Microsoft.NET.Sdk.Functions nuget 包(目前最新版本为1.0.29

    这是一个示例代码,请随时根据需要进行修改:

    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Hosting;
    using Microsoft.Extensions.Logging;
    using System.IO;
    
    [assembly: WebJobsStartup(typeof(FunctionApp16.MyStartup))]
    namespace FunctionApp16
    {
        public static class Function1
        {
            [FunctionName("Function1")]
            public static void Run("your parameters")
            {
                //your code here
            }
        }
    
        public class MyStartup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                //write your code here, it will executes prior to the function method.
            }
        }
    
    }
    

    【讨论】:

    • 我正在使用 NodeJs 。
    • @MILJOJOHN,对于node.js,我刚找到这个issue,你看看有没有用。
    • @MILJOJOHN,你能按照我在评论中提供的链接解决你的问题吗?
    【解决方案2】:

    Azure API 管理服务对所有传入请求启用 JWT 验证。

    validate-jwt 策略强制从指定的 HTTP 标头或指定的查询参数中提取的 JWT 的存在和有效性。

    在执行其他安全和授权用例时,这对于检查声明中的权限非常有用。您可以在文档here 中阅读更多内容。虽然这不会查询您的数据库以获得用户权限,但它是实现您的目标的一种非常有效的方法。

    这是文档中的语法。

      <required-claims>
        <claim name="name of the claim as it appears in the token" match="all|any" separator="separator character in a multi-valued claim">
          <value>claim value as it is expected to appear in the token</value>
          <!-- if there is more than one allowed values, then add additional value elements -->
        </claim>
        <!-- if there are multiple possible allowed values, then add additional value elements -->
      </required-claims>
    

    这里是你可以如何实现它。

      <required-claims>
        <claim name="FirstRoute" match="any">
          <value>true</value>
        </claim>
        <claim name="SecondRoute" match="any">
          <value>false</value>
        </claim>
      </required-claims>
    

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 1970-01-01
      • 2019-09-14
      • 2017-02-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2018-06-15
      相关资源
      最近更新 更多