中山模板建站软件,贵州seo排名,90设计官网,wordpress积分冻结小白开学Asp.Net Core 《八》— — .Net Core 数据保护组件1、背景我在搞#xff08;https://github.com/AjuPrince/Aju.Carefree#xff09;这个开源项目的时候#xff0c;想做一些防止恶意攻击的小功能#xff08;如果 我通过页面 /Dome/GetData?id123#xff0c;那是不… 小白开学Asp.Net Core 《八》 — — .Net Core 数据保护组件1、背景 我在搞https://github.com/AjuPrince/Aju.Carefree这个开源项目的时候想做一些防止恶意攻击的小功能如果 我通过页面 /Dome/GetData?id123那是不是不安全呢是的我完全可以尝试着给id赋值后去获取数据怎么办呢在.Net Core 中又给如何处理呢2、.Net Core 的数据保护组件 1、尝试着在.Net Core 的内部扩展方法中发现 我们都知道在 .Net Core 中注册服务都是在 Startup-ConfigureServices 这个方式中 通过 services.AddXXXX 来添加的我也尝试着看看 .Net Core 有无内置的数据保护组件就利用 VS的智能提示功能 输入 server.Add 一个个去看结果就被我我发现了开心地像个孩子 哈哈 F12 进去后通过它的注释Extension methods for setting up data protection services in an Microsoft.Extensions.DependencyInjection.IServiceCollection.(译成中文在Microsoft.Extensions.DependencyInjection.IServiceCollection设置数据保护服务的扩展方法)。好既然找到了那我们就来学习下它我们该如何使用它。 2、学习、使用 既然知道了(.Net Core 内置了数据保护组件)那我也就在类试图中去找它了最终还是被我给找见了。好不废话了 我们通过上图可以知道 .Net Core 内置了一个 IDataProtectionProvider 接口 和 IDataProtector 接口其中 IDataProtectionProvider 接口是创建保护组件的接口IDataProtector 是数据保护的接口我们可以实现这两个接口创建数据保护组件。 肯定有人问我我怎么知道的 同样的方法可以去看看另一个接口。下面就让我们来使用它。1)、新建一个类public class DataDemoViewModel { public int Id { get; set; } public string Name { get; set; } public DataDemoViewModel(int id, string name) { Id id; Name name; } }2、创建模拟数据public class DemoController : Controller { private ListDataDemoViewModel _listDataProtect new ListDataDemoViewModel(); public DemoController(){ //创建模拟数据 for (int i 0; i 6; i) { _listDataProtect.Add(new DataDemoViewModel(i, Aju_Carefree i)); } }
}3、在Startup类的ConfigureService方法中添加服务services.AddDataProtection();4、在DemoController中 DI注入public class DemoController : Controller { private ListDataDemoViewModel _listDataProtect new ListDataDemoViewModel(); private readonly IDataProtector _dataProtector; public DemoController(IDataProtectionProvider dataProtectionProvider) { //创建模拟数据 for (int i 0; i 6; i) { _listDataProtect.Add(new DataDemoViewModel(i, Aju_Carefree i)); } _dataProtector dataProtectionProvider.CreateProtector(aju_carefree_string); } }5、使用#region 数据保护组件Demo public IActionResult ProtectIndex() { var outputModel _listDataProtect.Select(item new { //使用 IDataProtector 接口的 Protect 方法对id字段进行加密 Id _dataProtector.Protect(item.Id.ToString()), item.Name }); return Ok(outputModel); } public IActionResult GetProtect(string id) { //使用 IDataProtector 接口的 Unprotect 方法对id字段进行解密 var orignalId int.Parse(_dataProtector.Unprotect(id)); var outputModel _listDataProtect.Where(s s.Id orignalId); return Ok(outputModel); } #endregion 6结果展示 1请求 /Demo/ProtectIndex 刷新页面id 值是变的。 2、请求 /Home/GetProtect?id(id取了上图中的第一个框框圈的)说明 1使用Provider创建Protector 的时候我们传入了一个参数“aju_carefree_string”这个参数标明了这个保护器的用途也可以当作这个保护器的名字。不同用途的保护器不能解密对方方加密的数据 2还有一个类型的数据保护组件ITimeLimitedDataProtector带过期时间的数据保护器就不在这里做说明了用法差不多。 3本篇文章只是对数据保护组件的抛砖引玉不只是说 它的用法就只能这么用完全可以有别的用法。 4本文的代码全部上传至Githubhttps://github.com/AjuPrince/Aju.CarefreeDemoController 参考文章 https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/introduction?viewaspnetcore-3.0 下一篇 需求了解些什么呢留言博客园哦我会从留言最多中选择一个内容来分享 我的看法及使用当然前提是我会哦 哈哈 本人有意组建兰州线下.Net 开发社区有意者加群QQ649708779如果条件允许的话将会在8月中旬组织个活动只是有这个想法