Http触发器Azure函数不应从App Service外部访问

时间:2020-11-03 16:15:04

标签: azure authentication azure-functions

我在我的App Service中使用了http触发天蓝色功能。我希望该http触发Azure功能不应公开访问,而只能从App Service访问。

当前,我已经为http触发功能创建了主机密钥,并将其用于身份验证请求。

我应该使用哪种身份验证方法?有什么想法。

天蓝色功能:

public static class RemoveSubscriptionsForPayers
    {
        [FunctionName(nameof(RemoveSubscriptionsForPayers))]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [Inject] ILoggingService loggingService,
            [Inject] ICommandValidator commandValidator,
            [Inject] ICommandHandler<ResultDto,RemoveSubscriptionsForPayersCommand> commandHandler)
        {
            var logger = new Logger(loggingService);

            try
            {
                IActionResult actionResult = null;

                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                logger.Info($"RemoveSubscriptionsForPayersCommand received on {nameof(RemoveSubscriptionsForPayers)}");

                var command = requestBody.AsPoco<RemoveSubscriptionsForPayersCommand>();

                if (commandValidator.Validate<RemoveSubscriptionsForPayersCommand>(req, command, new RemoveSubscriptionsForPayersCommandValidator(), logger, ref actionResult))
                {
                    var response =await commandHandler.HandleAsync(command, logger);
                    actionResult = new OkObjectResult(response);
                }

                return actionResult;
            }
            catch (Exception ex)
            {
                logger.Error($"Exception while processing {nameof(RemoveSubscriptionsForPayers)}", ex,
                  nameof(RemoveSubscriptionsForPayers));

                throw;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用Azure AD对功能进行身份验证,这更加安全。

enter image description here

enter image description here

打开Azure AD身份验证后,您需要获取访问令牌。

请在Azure门户中打开Azure active directory并找到App registrations,您需要在搜索框中搜索在Azure AD中注册的功能。

enter image description here

您需要在此处找到url和body的参数值以获得令牌。

URL to get access token

身体:

您可以这样获得令牌:

enter image description here

现在,您可以使用Azure AD的访问令牌来访问您的功能。

请求标头名称为Authorization,标头值为Bearer <access-token>

enter image description here