基本上HTTP是沒有記錄狀態的協定,但能夠經過Cookies將Request來源區分出來,並將部分數據暫存於Cookies及Session,是寫網站經常使用的用戶數據暫存方式。
本篇將介紹如何在ASP.NET Core使用Cookie及Session。git
Cookies是將用戶數據存在Client的瀏覽器,每次Request都會把Cookies送到Server。
在ASP.NET Core中要使用Cookie,能夠經過HttpContext.Request
及HttpContext.Response
存取:github
Startup.csweb
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是經過Cookies內的惟一識別信息,把用戶數據存在Server端內存、NoSQL或數據庫等。
要在ASP.NET Core使用Session須要先加入兩個服務:安全
IDistributedCache
物件,讓Session服務知道要將Session存在哪邊。IDistributedCache
分散式快取)Startup.cscookie
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 信息以下:session
能夠看到多出了.AspNetCore.Session
,.AspNetCore.Session
就是Session的惟一識別信息。
每次Request時都會帶上這個值,當Session服務取得這個值後,就會去Session容器找出專屬這個值的Session數據。app
之前ASP.NET能夠將對象直接存放到Session,如今ASP.NET Core Session再也不自動序列化對象到Sesson。
若是要存放對象到Session就要本身序列化了,這邊以JSON格式做爲範例:async
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
就能夠取到該用戶數據,也是有風險。
有些安全調整建議實做:
.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容器中加入IHttpContextAccessor
及ISessionWapper
,以下:
Startup.cs
// ... public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<ISessionWapper, SessionWapper>(); }
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