【问题标题】:.NET Core 3.1 Secure credentials when deploying to generic hosting.NET Core 3.1 部署到通用托管时的安全凭据
【发布时间】:2020-05-13 14:28:47
【问题描述】:

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

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

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

【问题讨论】:

    标签: security asp.net-core asp.net-core-3.1


    【解决方案1】:

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

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

    1. 另外,将一个纯文本文件放在我网站的根目录中,这样公众就无法访问它,然后在运行时将其拉入,是否可以节省?

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

    我的建议是使用一些第三方安全工具,如 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2021-06-10
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多