小白開學Asp.Net Core 《八》

小白開學Asp.Net Core 《八》

            — — .Net Core 數據保護組件

一、背景

  我在搞(https://github.com/AjuPrince/Aju.Carefree)這個開源項目的時候,想作一些防止惡意攻擊的小功能(若是 我經過頁面 /Dome/GetData?id=123,那是否是不安全呢?是的,我徹底能夠嘗試着給id賦值後去獲取數據)怎麼辦呢?在.Net Core 中又給如何處理呢?git

二、.Net Core 的數據保護組件

  一、嘗試着在.Net Core 的內部擴展方法中發現github

  咱們都知道在 .Net Core 中註冊服務,都是在 Startup->ConfigureServices 這個方式中 經過 services.AddXXXX 來添加的,我也嘗試着看看 .Net Core 有無內置的數據保護組件,就利用 VS的智能提示功能 輸入 server.Add 一個個去看,結果就被我我發現了(開心地像個孩子 哈哈)安全

              

F12 進去後ide

經過它的註釋(Extension methods for setting up data protection services in an Microsoft.Extensions.DependencyInjection.IServiceCollection.)(譯成中文:在Microsoft.Extensions.DependencyInjection.IServiceCollection設置數據保護服務的擴展方法)。學習

好,既然找到了,那咱們就來學習下它(咱們該如何使用它)。加密

  二、學習、使用spa

  

 既然知道了(.Net Core 內置了數據保護組件),那我也就在類試圖中去找它了,最終仍是被我給找見了。(好不廢話了)3d

  咱們經過上圖能夠知道 .Net Core 內置了一個 IDataProtectionProvider  接口 和 IDataProtector 接口,其中 IDataProtectionProvider 接口是建立保護組件的接口,IDataProtector 是數據保護的接口,咱們能夠實現這兩個接口,建立數據保護組件。code

  (確定有人問我,我怎麼知道的)server

 

 一樣的方法,能夠去看看,另外一個接口。

下面就讓咱們來使用它。

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月中旬,組織個活動(只是有這個想法)

相關文章
相關標籤/搜索