.NET Core 3.1 部署到通用托管时的安全凭据

.NET Core 3.1 Secure credentials when deploying to generic hosting

我的问题分为两部分,但它们都与安全凭证存储有关。背景是我正在使用 ftp 的基本托管服务来部署我的 .net core 3.1 站点。除了我部署文件的根文件夹之外,我没有目录访问权限。我正在使用 sql 服务器和其他一些需要我使用 API 密钥的第三方服务。

对于开发,来自不同第三方的所有文档都让我使用 windows 凭据存储或将文件放在解决方案文件夹之外的文件系统中的某个位置。但这不是部署的选项。

所以我的问题是,要部署到这个托管服务...
1. 使用 appsettings.json 存储我的 API 密钥是否安全?
2. 另外,是否只将一个平面文本文件放在我网站的根目录中,这样 public 就无法访问它,然后在运行时将其拉入?

1.Is 使用 appsettings.json 存储我的 API 密钥安全吗?

如果您使用第三方托管服务使用 FTP 到文件夹,假设只有您可以看到您的部署文件是不安全的。

  1. 此外,将一个平面文本文件放在我网站的根目录中,这样 public 就无法访问它,然后在运行时将其拉入,这样是否省事?

这与使用 appsettings.json 存储 API 密钥相同,因为应用程序设置文件也不应公开 public。

我可以建议的是使用一些第三方安全工具,如 Azure Vault 或 Hashi Vault 来存储秘密(它们在那里加密)并在您的应用程序中使用。

如果您不想使用第三方服务,您可以创建自定义配置生成器,您可以在保存前加密机密,使用前解密。

public class CustomConfigProvider : ConfigurationProvider
    {
        public CustomConfigProvider() { }

        public override void Load()
        {
            Data = MyEncryptUtils.DecryptConfiguration();
        }
}

public class CustomConfigurationSource : IConfigurationSource
{
    public CustomConfigurationSource() { }


    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new CustomConfigProvider();
    }
}

public class CustomConfigurationSource : IConfigurationSource
{
    public CustomConfigurationSource() { }


    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new CustomConfigProvider();
    }
}

启动时调用

var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddCustomConfiguration()
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

来源:https://stormpath.com/blog/store-protect-sensitive-data-dotnet-core

Steve 在这里也有使用桥接模式和加密的替代方法: https://stevetalkscode.co.uk/configuration-bridging-part-4