.net - 在 Azure Function 应用程序中使用 HSTS 响应 header

标签 .net azure azure-functions hsts

我需要在API(而不是Web应用程序)的Azure函数应用程序的响应 header 中发送HSTS header 。我能够添加具有所需选项的 HSTS,但无法使用 useHSTS,如下所示。

public override void Configure(IFunctionsHostBuilder builder)
{
   ....
   builder.Services.AddHsts(options =>
            {
                options.Preload = true;
                options.IncludeSubDomains = true;
                options.MaxAge = TimeSpan.FromDays(365);
            });
   ....
}

Startup.cs 文件不允许我使用参数为 IApplicationBuilderConfigure 方法。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
 
            }
            else
            {
                app.UseHsts();
 
            }

      }

任何在响应中获取 HSTS header 的建议都值得赞赏!

最佳答案

请检查此https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook?tabs=in-process%2Cfunctionsv2&pivots=programming-language-csharp#hostjson-settings

在host.json

{
    "version": "2.0",
    ...
    },
    "extensions": {
        "http": {
            "routePrefix": "api",
            "hsts": {
                "isEnabled": true,
                "includeSubDomains": true,
                "maxAge": "365"
            },
            "customHeaders": {
                "X-Content-Type-Options": "nosniff",
                "X-Frame-Options": "DENY",
                "X-XSS-Protection": "1; mode=block"
            }
        }
    }
}

关于.net - 在 Azure Function 应用程序中使用 HSTS 响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71275059/

相关文章:

json - 在 VS 2017 Azure Function 开发中从 local.settings.json 读取值

c# - 为什么 IDependencyResolver 与 System.Web 紧密耦合?

c# - 为 RequiredAttribute 提供自定义消息

Azure函数: Old code still running after a deployment

c# - 错误返回 "No job functions found. Try making your job classes and methods public"后,尝试在 Linux 中构建 Azure 函数应用程序

mysql - 无法将 Azure Web App - NodeJS 连接到 Azure Mysql

c# - .NET Core 上的 Azure Function v2 上的 EF Core 迁移问题

c# - 如何从Type对象获取可为null的Type对象

.net - 如果选中复选框,如何使文本框成为必需

azure - Azure DevOps 中的项目迁移