ASP.NET Core 2 學習筆記(十一)Cookies & Session

来源:https://www.cnblogs.com/snaildev/archive/2018/06/05/9138858.html
-Advertisement-
Play Games

基本上HTTP是沒有記錄狀態的協定,但可以通過Cookies將Request來源區分出來,並將部分數據暫存於Cookies及Session,是寫網站常用的用戶數據暫存方式。本篇將介紹如何在ASP.NET Core使用Cookie及Session。 Cookies Cookies是將用戶數據存在Cli ...


基本上HTTP是沒有記錄狀態的協定,但可以通過Cookies將Request來源區分出來,並將部分數據暫存於Cookies及Session,是寫網站常用的用戶數據暫存方式。
本篇將介紹如何在ASP.NET Core使用Cookie及Session。

Cookies

Cookies是將用戶數據存在Client的瀏覽器,每次Request都會把Cookies送到Server。
在ASP.NET Core中要使用Cookie,可以通過HttpContext.RequestHttpContext.Response存取:

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyWebsite
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // app.Run(async (context) =>
            // {
            //     await context.Response.WriteAsync("Hello World!");
            // });

            app.Run(async (context) =>
            {
                string message;

                if (!context.Request.Cookies.TryGetValue("Sample", out message))
                {
                    message = "Save data to cookies.";
                }
                context.Response.Cookies.Append("Sample", "This is Cookies.");
                // 刪除 Cookies 數據
                //context.Response.Cookies.Delete("Sample");

                await context.Response.WriteAsync($"{message}");
            });
        }
    }
}

從HTTP 可以看到傳送跟收到的Cookies 信息:

當存在Cookies 的信息越多,封包就會越大,因為每個Request 都會帶著Cookies 數據。

Session

Session是通過Cookies內的唯一識別信息,把用戶數據存在Server端記憶體、NoSQL或資料庫等。
要在ASP.NET Core使用Session需要先加入兩個服務:

  • Session容器
    Session可以存在不同的地方,透過DI IDistributedCache物件,讓Session服務知道要將Session存在哪邊。
    (之後的文章會介紹到IDistributedCache分散式快取)
  • Session服務
    在DI容器加入Session服務。並將Session的Middleware加入Pipeline。

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyWebsite
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // 將 Session 存在 ASP.NET Core 記憶體中
            services.AddDistributedMemoryCache();
            services.AddSession();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // SessionMiddleware 加入 Pipeline
            app.UseSession();

            app.Run(async (context) =>
            {
                context.Session.SetString("Sample", "This is Session.");
                string message = context.Session.GetString("Sample");
                await context.Response.WriteAsync($"{message}");
            });
        }
    }
}

HTTP Cookies 信息如下:

可以看到多出了.AspNetCore.Session.AspNetCore.Session就是Session的唯一識別信息。
每次Request時都會帶上這個值,當Session服務取得這個值後,就會去Session容器找出專屬這個值的Session數據。

對象類型

以前ASP.NET可以將對象直接存放到Session,現在ASP.NET Core Session不再自動序列化對象到Sesson。
如果要存放對象到Session就要自己序列化了,這邊以JSON格式作為範例:

Extensions\SessionExtensions.cs

using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace MyWebsite.Extensions
{
    public static class SessionExtensions
    {
        public static void SetObject<T>(this ISession session, string key, T value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T GetObject<T>(this ISession session, string key)
        {
            var value = session.GetString(key);
            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }
    }
}

通過上面擴展方法,就可以將對象存取至Session,如下:

using MyWebsite.Extensions;
using MyWebsite.Models;
// ...
var user = context.Session.GetObject<UserModel>("user");
context.Session.SetObject("user", user);

安全性

雖然Session數據都存在Server端看似安全,但如果封包被攔截,只要拿到.AspNetCore.Session就可以取到該用戶數據,也是有風險。
有些安全調整建議實作:

  • SecurePolicy
    限制只有在HTTPS連線的情況下,才允許使用Session。如此一來變成加密連線,就不容易被攔截。
  • IdleTimeout
    修改合理的Session到期時間。預設是20分鐘沒有跟Server互動的Request,就會將Session變成過期狀態。
    (20分鐘有點長,不過還是要看產品需求。)
  • Name
    沒必要將Server或網站技術的信息爆露在外面,所以預設Session名稱.AspNetCore.Session可以改掉。
// ...
public void ConfigureServices(IServiceCollection services)
{
    services.AddDistributedMemoryCache();
    services.AddSession(options =>
    {
        options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        options.Cookie.Name = "mywebsite";
        options.IdleTimeout = TimeSpan.FromMinutes(5);
    });
}

強類型

由於Cookies及Session預設都是使用字串的方式存取資料,弱類型無法在開發階段判斷有沒有打錯字,還是建議包裝成強類型比較好。
而且直接存取Cookies/Session的話邏輯相依性太強,對單元測試很不友善,所以還是建議包裝一下。

Wappers\SessionWapper.cs

using Microsoft.AspNetCore.Http;
using MyWebsite.Extensions;
using MyWebsite.Models;
// ...
namespace MyWebsite.Wappers
{
    public interface ISessionWapper
    {
        UserModel User { get; set; }
    }

    public class SessionWapper : ISessionWapper
    {
        private static readonly string _userKey = "session.user";
        private readonly IHttpContextAccessor _httpContextAccessor;

        public SessionWapper(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        private ISession Session
        {
            get
            {
                return _httpContextAccessor.HttpContext.Session;
            }
        }

        public UserModel User
        {
            get
            {
                return Session.GetObject<UserModel>(_userKey);
            }
            set
            {
                Session.SetObject(_userKey, value);
            }
        }
    }
}

在DI容器中加入IHttpContextAccessorISessionWapper,如下:

Startup.cs

// ...
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<ISessionWapper, SessionWapper>();
}
  • IHttpContextAccessor
    ASP.NET Core實現了IHttpContextAccessor,讓HttpContext可以輕鬆的註入給需要用到的對象使用。
    由於IHttpContextAccessor只是取用HttpContext實例的介面,用Singleton的方式就可以供其它物件使用。

在Controller就可以直接註入ISessionWapper,以強類型的方式存取Session,如下:

Controllers/HomeController.cs

using Microsoft.AspNetCore.Mvc;
using MyWebsite.Wappers;

namespace MyWebsite.Controllers
{
    public class HomeController : Controller
    {
        private readonly ISessionWapper _sessionWapper;

        public HomeController(ISessionWapper sessionWapper)
        {
            _sessionWapper = sessionWapper;
        }

        public IActionResult Index()
        {
            var user = _sessionWapper.User;
            if (user == null) user = new Models.UserModel();
            _sessionWapper.User = user;
            return Ok(user);
        }
    }
}

參考

Introduction to session and application state in ASP.NET Core

老司機發車啦:https://github.com/SnailDev/SnailDev.NETCore2Learning


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 如圖: 若幹倉庫都是能推不能拉,下麵顯示當前分支不跟蹤遠程分支,後來在這裡找到給分支建立跟蹤的方法,又在這裡找到在VS2017中敲git命令的方法,在此感謝上述博文的作者。總結一下: 在VS2017進入 > 【工具】菜單 > Nuget包管理器 > 程式包管理器控制台 在該控制臺中執行:git br ...
  • 剛好這次項目中遇到了這個東西,就來弄一下,挺簡單的,但是前臺調用的時候弄錯了,浪費了大半天的時間,本人也是菜鳥一枚。開始吧。(MVC的) @using Rattan.Core.Utility;@{ string ButtonScript = string.Empty;}@if (Rattan.Bas ...
  • 一、 樣式一 我們要實現上圖中的效果,需要如下的操作: 從工具欄上的“Smobiler Components”拖動一個VoiceRecorder控制項和一個ImageButton控制項到窗體界面上 修改ImageButton的屬性 1.BackColor屬性 設置控制項的背景色,將該屬性設置為“Gray” ...
  • select a,sys_guid() as b from mytable sys_guid() 是生成帶分隔符(-)的GUID的自定義函數 查詢B表的內容插入A表,MY_ID是A表的主鍵不可為空,因此需要B查詢出來的數據增加一列並且賦值插入A insert into A (plan_id,ship ...
  • 新建了一個類繼承EF Model類,運行報錯 EF Code First列名 'Discriminator' 無效 EF會把項目中在DbContext中引用的所有的Model類及這些Model類對應的子類都生成對應映射視圖。如果資料庫沒有對應表或欄位就會報錯。 在繼承的Model 類加上NotMap ...
  • 索引 NET Core應用框架之BitAdminCore框架應用篇系列 框架演示:http://bit.bitdao.cn 框架源碼:https://github.com/chenyinxin/cookiecutter-bitadmin-core 20180605更新內容 一、本次更新內容如下: 集 ...
  • 一、結論: 1.實例構造函數與靜態構造函數執行順序 一、初始化順序(依次是靜態變數、靜態構造函數、實例變數、實例構造函數) 二、初始化次數(靜態的都只會初始化一次) 三、初始化時機(使用類引用任何靜態成員之前對或者首次實例化類時會調用靜態構造函數,通過靜態構造函數初始化類級別的項,即靜態欄位。非靜態 ...
  • LRUCache是Least Recently Used 近期最少使用演算法的緩存,是android提供的一個緩存工具類。可以以兩種排序方式來輸出緩存,一種是按插入順序輸出,一種是按最近最少方式輸出,最近使用的放在隊首,使用頻率低的,間隔時間最長的放在隊尾。 下麵是實現 構造函數中傳入緩存大小和輸出緩 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...