隔离的Azure函数缺少身份验证标头?

发布于 2025-02-13 23:44:49 字数 1566 浏览 0 评论 0原文

我创建了一个新的.NET 6隔离的Azure函数,然后是这篇文章: https://joonasw.net/view/azure-ad-jwt-authentication-in-net-isolated-process-azure-functions

如果我在本地调试,它可以很好地工作,它可以很好地工作,客户端将身份验证标头附加到我可以在身份验证中间件中阅读的请求。但是,一旦将功能应用程序部署到Azure中,我将无法访问身份验证中间件中的身份验证标头,则缺少此标头条目。看起来身份验证标头以某种方式从标题中删除。

我的程序是

public static void Main()
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults(builder =>
            {
                builder.UseNewtonsoftJson();
                builder.UseMiddleware<AuthenticationMiddleware>();
                builder.UseMiddleware<AuthorizationMiddleware>();
                builder.UseMiddleware<ExceptionHandlerMiddleware>();
                builder.Services.AddOptions<AppSettings>()
                             .Configure<IConfiguration>((settings, configuration) =>
                             {
                                 configuration.GetSection("AppSettings").Bind(settings);
                             });
                builder.Services.AddPersistenceRepositories();
                builder.Services.AddPersistenceServices();
                builder.Services.AddPersistenceInfrastructures();
                builder.Services.AddSingleton<IHttpFunctionExecutor, HttpFunctionExecutor>();
            })
            .ConfigureOpenApi()
            .Build();

        host.Run();
    }

I have created a new .NET 6 Isolated Azure Function followed by this great article:
https://joonasw.net/view/azure-ad-jwt-authentication-in-net-isolated-process-azure-functions

If I'm debugging it locally, it works perfectly, the calling client attaches an authentication header to the request which I can read in the authentication middleware. But once the function app is deployed in Azure, I cannot access the authentication header in the authentication middleware, this header entry is missing. It looks like the authentication header is somehow removed from the header.

My program is

public static void Main()
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults(builder =>
            {
                builder.UseNewtonsoftJson();
                builder.UseMiddleware<AuthenticationMiddleware>();
                builder.UseMiddleware<AuthorizationMiddleware>();
                builder.UseMiddleware<ExceptionHandlerMiddleware>();
                builder.Services.AddOptions<AppSettings>()
                             .Configure<IConfiguration>((settings, configuration) =>
                             {
                                 configuration.GetSection("AppSettings").Bind(settings);
                             });
                builder.Services.AddPersistenceRepositories();
                builder.Services.AddPersistenceServices();
                builder.Services.AddPersistenceInfrastructures();
                builder.Services.AddSingleton<IHttpFunctionExecutor, HttpFunctionExecutor>();
            })
            .ConfigureOpenApi()
            .Build();

        host.Run();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2025-02-20 23:44:49

感谢您 @ jack.pop 的解决方案,发布与答案相同,以便其他社区成员可以为类似的问题提供好处。

解决方法: -

请确保将授权级别设置为 匿名 如图所示下面

 公共静态类测试功能
{
    [授权(
        scopes = new [] {“ access_as_user”},
        userroles = new [] {“ admin”})]
    公共静态httpresponsedata仅admins(
        [httptrigger(授权level.anonymous,“ get”)] httprequestdata req,
        functionContext executionContext)
    {
    }
}
 

有关更多信息,请参阅此博客| 隔离的Azure函数缺少身份验证标头和身份

Thank you @jack.pop ,For the solution ,Posting the same as answer so that other community members can beneficial for similar issue.

WORKAROUND:-

Please make sure to set the authorization level to Anonymous as shown below .

public static class TestFunctions
{
    [Authorize(
        Scopes = new[] { "access_as_user" },
        UserRoles = new[] { "admin" })]
    public static HttpResponseData OnlyAdmins(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
        FunctionContext executionContext)
    {
    }
}

For more information please refer this Blog|Isolated Azure Function missing authentication header and identity .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文