You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure App Service中移除Blazor Server的Appsettings值?

问题描述

Blazor Server应用部署到Azure App Service后,已通过Azure门户配置面板设置了应用配置且运行正常,但通过Kudo工具仍能看到包含本地开发配置的appsettings.json文件。尝试两种解决方案均失败:

  • 构建流水线移除appsettings.json,部署后出现500服务器错误,恢复文件后恢复正常;
  • 将配置移至appsettings.development.json,移除该文件并保留空appsettings.json,同样导致500错误。

代码中配置调用方式:

  • Program.cs:
var connString = builder.Configuration["SqlConnection"];
  • Razor组件:
IConfiguration config; // 依赖注入
_config = config;

_config["GroupName"]

本地appsettings.json内容:

{
  "GroupName": "MyGroup",
  "SqlConnection": "xxxxxxxxxxx",
  "OIDC": {
    "Authority": "xxxxxxx",
    "ClientId": "IDName",
    "ClientSecret": "",
    "CallbackPath": "/Index"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
解决方案

1. 保留appsettings.json但排除敏感配置与开发文件

ASP.NET Core配置加载有优先级,Azure App Service的应用配置(App Settings)会覆盖appsettings.json中的同名值。基于此,可以按以下步骤操作:

  • 将本地敏感配置(如SqlConnection、OIDC相关)移至appsettings.development.json,仅在本地开发时使用;
  • 修改appsettings.json,只保留非敏感的默认配置(如Logging、AllowedHosts),示例:
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
  • 在项目的.csproj文件中添加配置,确保发布时不包含appsettings.development.json:
<ItemGroup>
  <Content Update="appsettings.development.json">
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>

这样部署到Azure的appsettings.json仅包含非敏感默认值,敏感配置完全由Azure门户管理,且不会被发布到服务器。

2. 避免移除appsettings.json的原因

ASP.NET Core默认启动流程依赖appsettings.json作为基础配置源,即使文件为空,若不存在会导致配置加载失败,触发500错误。因此必须保留该文件,仅需清理其中的敏感内容即可。

3. 进一步隐藏敏感配置:使用Azure Key Vault

若需要更高安全性,可将SqlConnection、ClientSecret等敏感值存储到Azure Key Vault,然后在Azure App Service中配置Key Vault引用:

  • 在Key Vault中添加对应的机密;
  • 在App Service的“配置”面板中,添加应用设置,值格式为@Microsoft.KeyVault(SecretUri=密钥的URI);
  • 确保App Service有访问Key Vault的权限(通过托管身份配置)。
    这种方式下,敏感值不会出现在App Settings或appsettings.json中,进一步降低泄露风险。

内容的提问来源于stack exchange,提问作者Caverman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 22:12:39