1、Session文档介绍

  1. 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档;https://docs.microsoft.com/zh-cn/aspnet/core
  2. .Net core Session的官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/app-state
  3. .Net core Session Github源码 https://github.com/aspnet/Session

2、Session简单应用

2.1、在Startup类的ConfigureServices方法中添加

services.AddDistributedMemoryCache();
services.AddSession();

因为Session的服务端存储需要缓存,所以需要引入.Net core的缓存DistributedMemoryCache;

2.2、在Startup类的Configure方法中添加

app.UseSession();

2.3、使用(存储和获取)

// 存储
HttpContext.Session.Set("LoginId", System.Text.Encoding.Default.GetBytes("")); // 获取
HttpContext.Session.TryGetValue("LoginId", out byte[] byteLoginId);
var loginId = System.Text.Encoding.Default.GetString(byteLoginId); // LoginId="666";

3、源码分析图

4、源码分析

4.1、程序加载

4.1.1、在ConfigureServices中添加分布式缓存,services.AddDistributedMemoryCache();

微软官方建议使用AddDistributedMemoryCache,当然也可以使用AddDistributedRedisCache、AddDistributedSqlServerCache或者自定义缓存也是可以的;

如果是分布式系统或者SSO单点登录,建议使用分布式的缓存AddDistributedRedisCache,AddDistributedSqlServerCache;

缓存的官方文档https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/memory

4.1.2、在ConfigureServices中添加AddSession;

 public static IServiceCollection AddSession(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} services.AddTransient<ISessionStore, DistributedSessionStore>();
services.AddDataProtection();
return services;
} public static IServiceCollection AddSession(this IServiceCollection services, Action<SessionOptions> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
} services.Configure(configure);
services.AddSession(); return services;
}

AddSession为IServiceCollection的扩展方法,有1个重载(传入Session的设置,使用services.Configure(configure),加载设置);

services.AddDataProtection()注入数据加密解密DataProtection(),在加密解密SessionKey时使用;

services.AddTransient<ISessionStore, DistributedSessionStore>();注入DistributedSessionStore,其中的Create 方法用做创建Session,调用Create方法时执行new DistributedSession();  DistributedSession类中包含了对IDictionary<EncodedKey, byte[]>的增删改查;

4.1.3、在Configure中UseSession

 public static IApplicationBuilder UseSession(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
} return app.UseMiddleware<SessionMiddleware>();
} public static IApplicationBuilder UseSession(this IApplicationBuilder app, SessionOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
} return app.UseMiddleware<SessionMiddleware>(Options.Create(options));
}

UseSession为IApplicationBuilder的扩展方法,也有1个重载,同样也是加载Session的设置,使用Options.Create(options)结合中间件加载设置;

关于中间件可以参考文档 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware

SessionMiddleware.cs为Session的中间件;其中包含Session的核心代码,操作MVC之前和之后的代码都在中间件中;

4.2、SessionMiddleware.cs类解析

在SessionMiddleware中一个异步方法Invoke;主要逻辑中包含了注释,应该很好理解;

   /// <summary>
/// Invokes the logic of the middleware.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
/// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
public async Task Invoke(HttpContext context)
{
var isNewSessionKey = false;
Func<bool> tryEstablishSession = ReturnTrue;
var cookieValue = context.Request.Cookies[_options.Cookie.Name]; // 解密cookieValue
var sessionKey = CookieProtection.Unprotect(_dataProtector, cookieValue, _logger);
if (string.IsNullOrWhiteSpace(sessionKey) || sessionKey.Length != SessionKeyLength)
{ // 生成36位随机数
var guidBytes = new byte[];
CryptoRandom.GetBytes(guidBytes);
sessionKey = new Guid(guidBytes).ToString(); // 加密cookieValue
cookieValue = CookieProtection.Protect(_dataProtector, sessionKey); // 设置Cookie
var establisher = new SessionEstablisher(context, cookieValue, _options);
tryEstablishSession = establisher.TryEstablishSession;
isNewSessionKey = true;
} var feature = new SessionFeature();
// 创建Sessin放入 HttpContext Features
feature.Session = _sessionStore.Create(sessionKey, _options.IdleTimeout, _options.IOTimeout, tryEstablishSession, isNewSessionKey);
context.Features.Set<ISessionFeature>(feature); try
{
// 执行逻辑(MVC)之间
await _next(context);
// 执行逻辑(MVC)之后
}
finally
{
// 设置HttpContext Features为空
context.Features.Set<ISessionFeature>(null); if (feature.Session != null)
{
try
{
// Commit Session,把 IDictionary<EncodedKey, byte[]>中的值放入缓存
await feature.Session.CommitAsync(context.RequestAborted);
}
catch (OperationCanceledException)
{
_logger.SessionCommitCanceled();
}
catch (Exception ex)
{
_logger.ErrorClosingTheSession(ex);
}
}
}
}

4.3、DistributedSession.cs 类解析

在SessionMiddleware Invoke方法中,可以看到创建Session最终执行的是new DistributedSession();

此类就不做过多的介绍了,主要就是对IDictionary<EncodedKey, byte[]>增删改查,序列化值、从缓存中Load数据和把数据放入缓存中;

代码过多就不放置博客上,可移至github :https://github.com/aspnet/Session/blob/master/src/Microsoft.AspNetCore.Session/DistributedSession.cs

5、总结

1、在asp.net core中Session的代码还是比较简单的,运用操作也比较简单;

2、可以清楚的理解asp.net core中Session的原理;

3、可以学习其他生产随机数的方法;

4、可以学习在中间件中怎么运用设置(Options.Create(options)、services.Configure(configure));

5、知道了中间件的简单运用;

6、学写了Httpcontext Features 的简单运用,关于 HttpContext可以直接使用Session(HttpContext.Session)在讲asp.net core http时会详细介绍;

7、简单知道了对于缓存的获取和增加;

8、下一篇将分析 .net core configuration,敬请关注;

9、记得推荐评论,或者可以留言希望分析哪部分asp.net core的源码

[asp.net core 源码分析] 01 - Session的更多相关文章

  1. 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器

    1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...

  2. ASP.NET Core[源码分析篇] - WebHost

    _configureServicesDelegates的承接 在[ASP.NET Core[源码分析篇] - Startup]这篇文章中,我们得知了目前为止(UseStartup),所有的动作都是在_ ...

  3. ASP.NET Core[源码分析篇] - Authentication认证

    原文:ASP.NET Core[源码分析篇] - Authentication认证 追本溯源,从使用开始 首先看一下我们通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务 ...

  4. ASP.NET Core[源码分析篇] - 认证

    追本溯源,从使用开始 首先看一下我们的通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务,这里通过JWT的认证方式讲解 public void ConfigureServ ...

  5. ASP.NET Core[源码分析篇] - Startup

    应用启动的重要类 - Startup 在ASP.NET Core - 从Program和Startup开始这篇文章里面,我们知道了Startup这个类的重要性,它主要负责了: 配置应用需要的服务(服务 ...

  6. ASP.NET Core源码学习(一)Hosting

    ASP.NET Core源码的学习,我们从Hosting开始, Hosting的GitHub地址为:https://github.com/aspnet/Hosting.git 朋友们可以从以上链接克隆 ...

  7. ASP.NET MVC 源码分析(一)

    ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...

  8. ASP.NET MVC源码分析

    MVC4 源码分析(Visual studio 2012/2013) HttpModule中重要的UrlRoutingModule 9:this.OnApplicationPostResolveReq ...

  9. asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证

    原文:asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证 在前面的文章中我们曾经涉及到ControllerActionInvoker类GetPara ...

随机推荐

  1. .net core使用Ku.Core.Extensions.Layui实现layui表单渲染

    演示网站地址:http://layui.kulend.com/项目地址:https://github.com/kulend/Ku.Core.Extensions/tree/master/Ku.Core ...

  2. php 微信JS-SDK封装类

    JSSDK使用步骤: 步骤一:绑定域名 步骤二:引入JS文件 步骤三:通过config接口注入权限验证配置 步骤四:通过ready接口处理成功验证 步骤五:通过error接口处理失败验证 具体步骤方法 ...

  3. 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用FairyGUI (一)

    我们的热更新脚本在实际使用中,当然也要支持常用的第三方组件,例如这里介绍一个非常实用的第三方UI库:FairyGUI. 什么是FairyGUI 这里照搬FaiyGUI官网的介绍: 重新定义 UI 制作 ...

  4. CentOS的启动流程

    因6和7俩个系列的启动流程有区别,所以我把他们分开来写 linux可看作是内核和根文件系统组成我们把内核单独拿出来总结一下 CentOS6系列启动流程 首先总结一下总体的流程,接下来展开来叙述:POS ...

  5. 功能式Python中的探索性数据分析

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 这里有一些技巧来处理日志文件提取.假设我们正在查看一些Enterprise Splunk提取.我们可以用Splunk来探索数据.或者我们可以 ...

  6. 敏捷(Agile)——“说三道四”

    可以这么理解:一种以人为本.团队合作.快速响应变化和可工作的软件作为宗旨的开发方法.亦可理解为在一个高度协作的环境中,不断地使用反馈进行自我调整和完善,持续交付用户想要的软件的过程.敏捷开发提倡通过多 ...

  7. Lucene入门案例一

    1. 配置开发环境 官方网站:http://lucene.apache.org/ Jdk要求:1.7以上 创建索引库必须的jar包(lucene-core-4.10.3.jar,lucene-anal ...

  8. java中Iterator和ListIterator的区别与联系

    首先,为什么需要使用迭代器Iterator,原有的for循环和for each不能满足需求吗? 那是因为Iterator模式是用于遍历集合类的标准访问方法.它可以把访问逻辑从不同类型的集合类中抽象出来 ...

  9. Collections.synchronizedMap()、ConcurrentHashMap、Hashtable之间的区别

    为什么要比较Hashtable.SynchronizedMap().ConcurrentHashMap之间的关系?因为常用的HashMap是非线程安全的,不能满足在多线程高并发场景下的需求. 那么为什 ...

  10. Activity,Window,View之间是什么关系?

    Activity在onCreate之前调用attach方法,在attach方法中会创建window对象.window对象创建时并没有创建 Decor对象对象.用户在Activity中调用setCont ...