1. 程式人生 > >ASP.NET Core 2 學習筆記(十一)Cookies & Session

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

自動 asp Go 內存 rtu serialize .html call names

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

基本上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

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