【问题标题】:Encrypt sections of the appsettings.json inside my Asp.Net Core MVC web apllication在我的 Asp.Net Core MVC Web 应用程序中加密 appsettings.json 的部分
【发布时间】:2020-10-14 23:20:33
【问题描述】:

我的 asp.net 核心 MVC Web 应用程序中有以下 appsettings.json:-

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\ProjectsV13;Database=LandingPage;Trusted_Connection=True;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "SMTP": {"
    "pass": "************"
  }
}

我在哪里存储 smtp 密码 + 当我将应用程序发布到共享主机提供商时,我将在 appsettings.json 的连接字符串中添加一个 sql server 用户名和密码。

所以我的问题是如何加密我的appsettings.json 内托管在 IIS 内的远程共享托管服务提供商内的部分?我可以将密码保存在 Visual Studio 项目中,但我想加密托管的 appsettings.json?这可能吗?

谢谢

【问题讨论】:

  • @शेखर 感谢您的链接,但它没有提到我们如何加密 appsettings.json 的部分

标签: asp.net asp.net-core iis encryption asp.net-core-mvc


【解决方案1】:

我如何加密托管在我的 appsettings.json 中的部分 IIS 内的远程共享主机提供商?我可以保留 Visual Studio项目中的密码,但我想加密 托管 appsettings.json?这可能吗?

首先,您需要以某种方式encrypt the password。在这里你有很多选择。

官方建议是这样使用Data Protection

string encrytedStr =  _protector.Protect("Unencrypted string");

然后,在您的项目中,您会在 appsettings.json 文件下找到一个 appsettings.Development.json 文件。

默认情况下,开发环境中使用的变量存放在appsettings.Development.json中,而生产环境中使用的变量存放在appsettings.json文件中。

所以你可以把加密后的内容放在appsettings.json文件中,把未加密的密码放在appsettings.Development.json文件中。

并且保证他们在json中的key是一样的,但是值一个是加密的,另一个是未加密的。

然后,在调用密码的控制器中,注入DataProtectionIHostingEnvironmentIConfiguration三个服务,然后在获取值之前判断环境是生产环境还是开发环境,再决定是否解密该值。

using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; 

namespace WebApplication_core_new.Controllers
{
    public class DefaultController : Controller
    {
        private readonly string _environmentName;
        private readonly IConfiguration _configuration;
        private readonly IDataProtector _protector;
        public DefaultController(IHostingEnvironment hostingEnvironment, IConfiguration configuration, IDataProtectionProvider provider)
        {
            _protector = provider.CreateProtector(GetType().FullName);
            _environmentName = hostingEnvironment.EnvironmentName;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            // here you can get the data in appsetting.json
            string data = _environmentName == "Development" ? _configuration["MySercet"] : _protector.Unprotect(_configuration["MySercet"]);
            return View();
        }
    }
}

在 appsetting.json 文件中:

{
  //... 
  "MySercet": "Encrypted string"
}

在 appsetting.Development.json 文件中:

 {
      //... 
      "MySercet": "Unencrypted string"
 }

【讨论】:

  • 感谢您的回复,但我不确定如何加密密码?当我们使用 web.config 时,我们使用这个命令ASPNET_REGIIS... 来加密 web.config 的一部分。所以我们可以在 appsettings.json 中这样做吗?
  • @testtest,不,我们不能。 ASPNET_REGIIS... 只能在 .NET Framework 而非核心中使用。至于加密方式,Data Protection是官方推荐的选项,我想你可以试试。
  • 感谢您的回复,但Data Protection is the officially recommended option, 到底是什么意思
  • @testtest,表示Data Protection的加密方式在官方文档中有描述:docs.microsoft.com/en-us/aspnet/core/security/data-protection/…,大家可以试试。
  • 不好的例子。不回答问题。如何在 Startup 中使用 IDataProtectionProvider ?
猜你喜欢
  • 2016-12-13
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多