• ASP.NET Core-数据保护(Data Protection)


    介绍:

     ASP.NET Core 数据保护堆栈提供简单易用的加密 API,开发人员可以使用它来保护数据,包括密钥管理和旋转。 

    Data Protection(数据安全)机制:为了确保Web应用敏感数据的安全存储,该机制提供了一个简单、基于非对称加密改进的加密API用于数据保护。
    它不需要开发人员自行生成密钥,它会根据当前应用的运行环境,生成该应用独有的一个私钥。

    ConfigureService()方法添加数据保护服务:

    string applicationName = $"FAN.APP";
                //添加数据保护服务,设置统一应用程序名称和加密方式
                IDataProtectionBuilder dataProtectionBuilder = services
                    .AddDataProtection(options => options.ApplicationDiscriminator = applicationName)
                    .SetApplicationName(applicationName)
                    .SetDefaultKeyLifetime(TimeSpan.FromDays(7))//<expirationDate>最小7天</expirationDate>
                    .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration
                    {
                        EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                        ValidationAlgorithm = ValidationAlgorithm.HMACSHA512
                    });
                return dataProtectionBuilder;

    加密、解密:

    public class HomeController : Controller
        {
            private IDataProtector _dataProtector = null;
    
            public HomeController(IDataProtectionProvider protectionProvider)
            {
                _dataProtector = protectionProvider.CreateProtector("aaa");
            }
    
            public IActionResult Index()
            {
                var protectedPayload = _dataProtector.Protect("haha");//加密
                var unprotectedPayload = _dataProtector.Unprotect(protectedPayload);//解密
                return View();
            }
    }

    私钥共享:

    这在单一部署的情况下没有问题。在集群情况下,为了确保加密数据的互通,应用必须共享私钥。 

    秘钥路径:%HOME%AppDataLocalASP.NETDataProtection-Keys

    这里以使用Redis来共享私钥举例,添加Microsoft.AspNetCore.DataProtection.StackExchangeRedis Nuget包用于存储密钥。
    添加Microsoft.Extensions.Caching.StackExchangeRedisNuget包用于配置分布式Session

        public void ConfigureServices(IServiceCollection services)
            {
                ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("xxxxxx:6379,defaultDatabase=10,password=xxxxxxx");
                string applicationName = "FAN.APP";
                services.AddDataProtection(o =>
                {
                    o.ApplicationDiscriminator = applicationName;
                })
                    //.PersistKeysToFileSystem(new System.IO.DirectoryInfo("c:\keys"))//秘钥存储文件位置
                    //.PersistKeysToRegistry(Microsoft.Win32.RegistryKey.FromHandle(null))//秘钥存储到注册表
                    .PersistKeysToStackExchangeRedis(connectionMultiplexer, "FAN_share_key")//秘钥存储到Redis中
                    .SetApplicationName(applicationName)//设置程序唯一标识
                    .SetDefaultKeyLifetime(TimeSpan.FromDays(14))//设置key的有效时间,到期刷新
                    .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
                    {
                        EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                        ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
                    });
    
                services.AddControllersWithViews();
    
            }

    参考:

    https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.1

     https://www.cnblogs.com/lwqlun/p/9726191.html

    https://www.cnblogs.com/savorboard/p/dotnetcore-data-protection.html

    https://www.cnblogs.com/savorboard/p/dotnet-core-data-protection.html

    https://www.cnblogs.com/savorboard/p/dotnetcore-data-protected-farm.html

  • 相关阅读:
    English trip V1
    English trip M1
    every day a practice —— morning(5)
    English Voice of <<All Of Me>>
    bzoj 3561 DZY Loves Math VI
    luogu P4322 [JSOI2016]最佳团体
    luogu P3264 [JLOI2015]管道连接
    bzoj 5084 hashit
    luogu P6091 原根
    bzoj 5206 [Jsoi2017]原力
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/12574125.html
Copyright © 2020-2023  润新知