ASP.NET Core 2 学习笔记(十一)Cookies & Session

基本上HTTP是没有记录状态的协定,但能够经过Cookies将Request来源区分出来,并将部分数据暂存于Cookies及Session,是写网站经常使用的用户数据暂存方式。
本篇将介绍如何在ASP.NET Core使用Cookie及Session。git

Cookies

Cookies是将用户数据存在Client的浏览器,每次Request都会把Cookies送到Server。
在ASP.NET Core中要使用Cookie,能够经过HttpContext.RequestHttpContext.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

Session是经过Cookies内的惟一识别信息,把用户数据存在Server端内存、NoSQL或数据库等。
要在ASP.NET Core使用Session须要先加入两个服务:安全

  • Session容器
    Session能够存在不一样的地方,透过DI IDistributedCache物件,让Session服务知道要将Session存在哪边。
    (以后的文章会介绍到IDistributedCache分散式快取)
  • Session服务
    在DI容器加入Session服务。并将Session的Middleware加入Pipeline。

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就能够取到该用户数据,也是有风险。
有些安全调整建议实做:

  • 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

相关文章
相关标签/搜索