加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

[asp.net core 源码分析] 01 - Session

发布时间:2020-12-15 21:24:16 所属栏目:asp.Net 来源:网络整理
导读:?1、Session文档介绍 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档;; .Net core Session的官方文档? .Net core Session Github源码? 2、Session简单应用 2.1、在Startup类的ConfigureServices方法中添加 因为Session的服务端存储

?1、Session文档介绍

  1. 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档;;
  2. .Net core Session的官方文档?
  3. .Net core Session Github源码?

2、Session简单应用

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

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

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

app.UseSession();

2.3、使用(存储和获取)

HttpContext.Session.Set(,System.Text.Encoding.Default.GetBytes(<span style="color: #008000;">//<span style="color: #008000;"> 获取
HttpContext.Session.TryGetValue(<span style="color: #800000;">"
<span style="color: #800000;">LoginId
<span style="color: #800000;">"
,<span style="color: #0000ff;">out
<span style="color: #0000ff;">byte
<span style="color: #000000;">[] byteLoginId);
<span style="color: #0000ff;">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;

缓存的官方文档

4.1.2、在ConfigureServices中添加AddSession;

IServiceCollection AddSession( (services == services.AddTransient IServiceCollection AddSession( IServiceCollection services,Action (services == (configure == }

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

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

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

?4.1.3、在Configure中UseSession

IApplicationBuilder UseSession( (app == app.UseMiddleware IApplicationBuilder UseSession( (app == (options == app.UseMiddleware }

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

关于中间件可以参考文档?

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

4.2、SessionMiddleware.cs类解析

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

isNewSessionKey = Func<> tryEstablishSession = cookieValue = sessionKey = (.IsNullOrWhiteSpace(sessionKey) || sessionKey.Length != guidBytes = [ sessionKey = cookieValue = establisher = tryEstablishSession = isNewSessionKey = feature = feature.Session = context.Features.Set context.Features.Set( (feature.Session != 中的值放入缓存 }

?4.3、DistributedSession.cs 类解析

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

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

代码过多就不放置博客上,可移至github :

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的源码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读