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

 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("666"));

// 获取
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;

 1 public static IServiceCollection AddSession(this IServiceCollection services)
 2 {
 3     if (services == null)
 4     {
 5         throw new ArgumentNullException(nameof(services));
 6     }
 7 
 8     services.AddTransient<ISessionStore, DistributedSessionStore>();
 9     services.AddDataProtection();
10     return services;
11 }
12  
13 public static IServiceCollection AddSession(this IServiceCollection services, Action<SessionOptions> configure)
14 {
15     if (services == null)
16     {
17         throw new ArgumentNullException(nameof(services));
18     }
19 
20     if (configure == null)
21     {
22         throw new ArgumentNullException(nameof(configure));
23     }
24 
25     services.Configure(configure);
26     services.AddSession();
27 
28     return services;
29 }
View Code

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

 1 public static IApplicationBuilder UseSession(this IApplicationBuilder app)
 2 {
 3     if (app == null)
 4     {
 5         throw new ArgumentNullException(nameof(app));
 6     }
 7 
 8     return app.UseMiddleware<SessionMiddleware>();
 9 }
10 
11 
12 public static IApplicationBuilder UseSession(this IApplicationBuilder app, SessionOptions options)
13 {
14     if (app == null)
15     {
16         throw new ArgumentNullException(nameof(app));
17     }
18     if (options == null)
19     {
20         throw new ArgumentNullException(nameof(options));
21     }
22 
23     return app.UseMiddleware<SessionMiddleware>(Options.Create(options));
24 }
View Code

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;主要逻辑中包含了注释,应该很好理解;

 1   /// <summary>
 2         /// Invokes the logic of the middleware.
 3         /// </summary>
 4         /// <param name="context">The <see cref="HttpContext"/>.</param>
 5         /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
 6         public async Task Invoke(HttpContext context)
 7         {
 8             var isNewSessionKey = false;
 9             Func<bool> tryEstablishSession = ReturnTrue;
10             var cookieValue = context.Request.Cookies[_options.Cookie.Name];
11 
12             // 解密cookieValue
13             var sessionKey = CookieProtection.Unprotect(_dataProtector, cookieValue, _logger);
14             if (string.IsNullOrWhiteSpace(sessionKey) || sessionKey.Length != SessionKeyLength)
15             {
16 
17                 // 生成36位随机数
18                 var guidBytes = new byte[16];
19                 CryptoRandom.GetBytes(guidBytes);
20                 sessionKey = new Guid(guidBytes).ToString();
21 
22                 // 加密cookieValue
23                 cookieValue = CookieProtection.Protect(_dataProtector, sessionKey);
24 
25                 // 设置Cookie
26                 var establisher = new SessionEstablisher(context, cookieValue, _options);
27                 tryEstablishSession = establisher.TryEstablishSession;
28                 isNewSessionKey = true;
29             }
30 
31             var feature = new SessionFeature();
32             // 创建Sessin放入 HttpContext Features
33             feature.Session = _sessionStore.Create(sessionKey, _options.IdleTimeout, _options.IOTimeout, tryEstablishSession, isNewSessionKey);
34             context.Features.Set<ISessionFeature>(feature);
35 
36             try
37             {
38                 // 执行逻辑(MVC)之间
39                 await _next(context);
40                 // 执行逻辑(MVC)之后
41             }
42             finally
43             {
44                 // 设置HttpContext Features为空
45                 context.Features.Set<ISessionFeature>(null);
46 
47                 if (feature.Session != null)
48                 {
49                     try
50                     {
51                         // Commit Session,把 IDictionary<EncodedKey, byte[]>中的值放入缓存
52                         await feature.Session.CommitAsync(context.RequestAborted);
53                     }
54                     catch (OperationCanceledException)
55                     {
56                         _logger.SessionCommitCanceled();
57                     }
58                     catch (Exception ex)
59                     {
60                         _logger.ErrorClosingTheSession(ex);
61                     }
62                 }
63             }
64         }
View Code

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

posted @ 2018-08-09 09:12  Emrys5  阅读(4122)  评论(12编辑  收藏  举报