Azure Functions v4 中的自定义授权与 .NET 7 隔离

问题描述 投票:0回答:1

我正在尝试使用 .NET 7 隔离进程在 Azure Functions v4 中实现自定义授权。以前,有 FunctionExecutingContext 可以与属性一起使用来处理自定义授权逻辑,但现在已标记为过时。

// Sample of the old approach
public sealed class AuthorizeAttribute : FunctionInvocationFilterAttribute
{
   public override void OnExecuting(FunctionExecutingContext executingContext)
   {
       // ... logic here ...
   }
}

不再建议这样做,在 Azure Functions v4 中处理此问题并隔离 .NET 6 的新方法是什么?

任何指导或示例实施将不胜感激!

authentication azure-functions custom-attributes authorize-attribute azure-functions-isolated
1个回答
0
投票

不要使用

FunctionExecutingContext
,而是使用
you can utilise FunctionContext in Azure Functions v4 with a.NET 7 isolated process to manage specific authorization

HttpRequestData 对象保存当前调用函数的 HTTP 请求信息,可通过 FunctionContext 类访问。要向您的函数代码添加独特的权限逻辑,请使用此对象。

参考1文档开始使用.Net 7 Isolate Functions

.Net 7 独立框架中的示例 Http 函数代码:-

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp44
{
    public class Function1
    {
        private readonly ILogger _logger;

        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, FunctionContext executioncontext)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
    }
}

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.