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