【问题标题】:Http Trigger Azure function should not accessible outside from App ServiceHttp Trigger Azure 函数不应从应用服务外部访问
【发布时间】:2021-02-16 08:21:56
【问题描述】:

我在我的应用服务中使用 http trigger azure 函数。我希望这个 http 触发 azure 函数不能公开访问,只能从应用服务访问。

目前我已经为 http 触发功能创建了主机密钥,我正在使用它来验证请求。

我应该使用哪种身份验证方法?任何想法。

Azure 函数:

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;
            }
        }
    }

【问题讨论】:

  • Azure AD 不能解决您的问题吗?
  • 谢谢!期待你的答复。我还没试过。

标签: azure authentication azure-functions


【解决方案1】:

您可以使用 Azure AD 对您的函数进行身份验证,这样更安全。

开启Azure AD认证后,需要获取访问令牌。

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

这里需要找到url和body的参数值来获取token。

URL to get access token

主体:

你可以这样获取Token:

现在您可以使用 Azure AD 的访问令牌来访问您的函数。

请求头名称为Authorization,头值为Bearer &lt;access-token&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2011-06-23
    • 2022-06-14
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多