ASP.NET Core中的加密配置

随着web.config去了,什么是存储敏感信息(密码,令牌)使用ASP.NET核心内建一个web应用程序的配置首选的方法是什么?

有没有一种方法可以自动获取加密的配置部分appsetttings.json


MMTTMM
浏览 894回答 3
3回答

茅侃侃

我不想编写自定义提供程序-太多的工作。我只是想利用JsonConfigurationProvider,所以我想出了一种对我有用的方法,希望它能对某人有所帮助。public class JsonConfigurationProvider2 : JsonConfigurationProvider{    public JsonConfigurationProvider2(JsonConfigurationSource2 source) : base(source)    {    }    public override void Load(Stream stream)    {        // Let the base class do the heavy lifting.        base.Load(stream);        // Do decryption here, you can tap into the Data property like so:         Data["abc:password"] = MyEncryptionLibrary.Decrypt(Data["abc:password"]);        // But you have to make your own MyEncryptionLibrary, not included here    }}public class JsonConfigurationSource2 : JsonConfigurationSource{    public override IConfigurationProvider Build(IConfigurationBuilder builder)    {        EnsureDefaults(builder);        return new JsonConfigurationProvider2(this);    }}public static class JsonConfigurationExtensions2{    public static IConfigurationBuilder AddJsonFile2(this IConfigurationBuilder builder, string path, bool optional,        bool reloadOnChange)    {        if (builder == null)        {            throw new ArgumentNullException(nameof(builder));        }        if (string.IsNullOrEmpty(path))        {            throw new ArgumentException("File path must be a non-empty string.");        }        var source = new JsonConfigurationSource2        {            FileProvider = null,            Path = path,            Optional = optional,            ReloadOnChange = reloadOnChange        };        source.ResolveFileProvider();        builder.Add(source);        return builder;    }}
打开App,查看更多内容
随时随地看视频慕课网APP