小白开学Asp.Net Core 《八》

小白开学Asp.Net Core 《八》

            — — .Net Core 数据保护组件

1、背景

  我在搞(https://github.com/AjuPrince/Aju.Carefree)这个开源项目的时候,想做一些防止恶意攻击的小功能(如果 我通过页面 /Dome/GetData?id=123,那是不是不安全呢?是的,我完全可以尝试着给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 List<DataDemoViewModel> _listDataProtect = new List<DataDemoViewModel>();

   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 List<DataDemoViewModel> _listDataProtect = new List<DataDemoViewModel>();

        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):本文的代码全部上传至Github(https://github.com/AjuPrince/Aju.Carefree)(DemoController )

 参考文章:

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

 

  如果觉得写的还不错的话,就点个推荐呗! 哈哈

 下一篇 需求了解些什么呢?留言哦!(我会从留言最多中选择一个内容来分享 我的看法及使用(当然前提是我会哦 哈哈))

   本人有意组建兰州线下.Net 开发社区,有意者加群(QQ:649708779)如果条件允许的话,将会在8月中旬,组织个活动(只是有这个想法)

 

posted @ 2019-07-16 11:23  ZerekZhang  阅读(1179)  评论(4编辑  收藏  举报