Azure Functions - 从 Azure 门户内部调用时出现 403 禁止错误

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

我尝试通过 Azure 门户执行计时器触发的功能,但收到 403 Forbidden 错误:

enter image description here

我在 Function App 上的网络设置: enter image description here

所以它应该可用并且应用程序服务本身正在运行。 我在 Azure 上使用默认设置创建了一个新的 Function App,但仍然收到 403 错误。 Azure 中是否有任何其他设置需要我手动设置才能执行该函数?

程序.cs

var host = new HostBuilder()
 .ConfigureAppConfiguration((context, config) =>
 {
     var builtConfig = config.Build();

     if (context.HostingEnvironment.IsDevelopment())
     {
         config.AddUserSecrets<Program>();
     }
 })
.ConfigureFunctionsWebApplication()
.ConfigureServices((context, services) =>
{
    var configuration = context.Configuration;
    var connectionString = configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");


    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
    services.ConfigureFunctionServices();
    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(connectionString));
    
    services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

    services.AddAutoMapper(typeof(Program));
    var mapperConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new ConfigureAutoMapper());
    });
})
.Build();
host.Run();

功能测试:

public class FunctionTest
{
    private readonly ILogger _logger;

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

[Function("FunctionTest")]
public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    
    if (myTimer.ScheduleStatus is not null)
    {
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

}

azure azure-functions http-status-code-403
1个回答
0
投票

更新以下代码后,我成功执行了计时器触发器功能,以从

local.settings.json
检索配置详细信息,这使其可以直接访问连接字符串。

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
      .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
      .AddEnvironmentVariables();
var connectionString = configuration["DefaultConnection"];

程序.cs:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using Microsoft.AspNetCore.Identity;
using Microsoft.Azure.Functions.Worker;

namespace FunctionApp16
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureAppConfiguration((context, config) =>
                {
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                          .AddEnvironmentVariables();

                    if (context.HostingEnvironment.IsDevelopment())
                    {
                        config.AddUserSecrets<Program>();
                    }
                })
                .ConfigureFunctionsWebApplication()
                .ConfigureServices((context, services) =>
                {
                    var configuration = context.Configuration;
                    var connectionString = configuration["DefaultConnection"];
                    if (string.IsNullOrEmpty(connectionString))
                    {
                        throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
                    }
                    services.AddApplicationInsightsTelemetryWorkerService();
                    services.AddDbContext<ApplicationDbContext>(options =>
                        options.UseSqlServer(connectionString));
                    services.AddIdentity<ApplicationUser, IdentityRole>()
                            .AddEntityFrameworkStores<ApplicationDbContext>()
                            .AddDefaultTokenProviders();
                    var mapperConfig = new MapperConfiguration(mc =>
                    {
                        mc.AddProfile(new ConfigureAutoMapper());
                    });
                    IMapper mapper = mapperConfig.CreateMapper();
                    services.AddSingleton(mapper);
                })
                .ConfigureLogging(logging =>
                {
                    logging.AddConsole();
                })
                .Build();
            host.Run();
        }
    }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<StorageConneString>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "DefaultConnection": "<sqlConneString>"
  }
}

我在功能应用程序>环境变量>应用程序设置中添加了DefaultConnection,如下所示。

enter image description here

Azure Function 应用程序输出:

Timer触发函数运行成功如下图。

enter image description here

祈求:

enter image description here

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