第十六节:Asp.Net Core中Session的使用、扩展、进程外Session

一. 简介

   关于Session的原理可参照Asp.Net版本Session的文章,去查阅。

1. 普通用法

 (1).通过Nuget引入【Microsoft.AspNetCore.Http】程序集,Core Mvc中已经默认引入了,在哪使用using一下即可。

 (2).注入Session相关的服务。

  A:在ConfigureService注册基于服务器内存的Session, services.AddDistributedMemoryCache(); services.AddSession(); 在Configure中进行启用Session,app.UseSession();

    注:一定要在mvc前面进行注入,进程外Session的注入后面介绍。

  B:如果项目中使用了CookiePolicyOptions,一定要将里面true改为false,如: options.CheckConsentNeeded = context => false; 或者配置Session的性质,options.Cookie.IsEssential = true;(表示cookie是必须的),否则chrome中拿不到Session值

 解释:这是个GDRP条例,让用户自己选择使用用cookie,详见 http://www.zhibin.org/archives/667 或 https://www.cnblogs.com/GuZhenYin/p/9154447.html

  C:在控制器中通过 HttpContext.Session 来向Session中读写各种值。

注册和启用相关的代码:

 1  public void ConfigureServices(IServiceCollection services)
 2  {
 3             services.Configure<CookiePolicyOptions>(options =>
 4             {
 5                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.
 6                 options.CheckConsentNeeded = context => false;       //改为false或者直接注释掉,上面的Session才能正常使用
 7                 options.MinimumSameSitePolicy = SameSiteMode.None;
 8             });
 9             //注册Session服务
10             //基于内存的Session
11             services.AddDistributedMemoryCache();
12             //默认过期时间为20分钟,每次访问都会重置
13             services.AddSession();
14 
15             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
16   }
17   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
18   {
19             if (env.IsDevelopment())
20             {
21                 app.UseDeveloperExceptionPage();
22             }
23             else
24             {
25                 app.UseExceptionHandler("/Home/Error");
26             }
27             app.UseStaticFiles();
28             app.UseCookiePolicy();
29             //启用Session管道
30             app.UseSession();
31 
32             app.UseMvc(routes =>
33             {
34                 routes.MapRoute(
35                     name: "default",
36                     template: "{controller=CoreSession}/{action=Index6}/{id?}");
37             });
38   }

测试代码:

 1       public IActionResult Index()
 2         {
 3             string userName = HttpContext.Session.GetString("userName");
 4             if (string.IsNullOrEmpty(userName))
 5             {
 6                 userName = "ypf";
 7                 HttpContext.Session.SetString("userName", userName);
 8             }
 9             var t1 = HttpContext.Session.GetString("userName");
10             ViewBag.userName = userName;
11             return View();
12         }
13 
14         public IActionResult Index2()
15         {
16             string userName = HttpContext.Session.GetString("userName");
17             ViewBag.userName = userName;
18             return View();
19         }

补充Session默认提供的方法:

2. 全局注册使用

(1).背景:通常便于管理,我们将Session进行全局统一注入,方便管理。

(2).步骤:

  A: 在普通用法的前提下,在ConfigureService中进行统一对象的注册, services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

  B:在控制器利用构造函数进行注入。

  C:正常使用即可,案例详见项目的Index3和Index4

代码分享:

 

 1  public class CoreSessionController : Controller
 2     {
 3         private readonly IHttpContextAccessor httpContextAccessor;
 4 
 5         private ISession MySession => httpContextAccessor.HttpContext.Session;
 6 
 7         public CoreSessionController(IHttpContextAccessor httpContextAccessor)
 8         {
 9             this.httpContextAccessor = httpContextAccessor;
10         }
11         #region 02-测试统一注入的用法
12         /// <summary>
13         /// 下面是统一注入使用的写法
14         /// </summary>
15         /// <returns></returns>
16 
17         public IActionResult Index3()
18         {
19             string userName = MySession.GetString("userName");
20             if (string.IsNullOrEmpty(userName))
21             {
22                 userName = "ypf";
23                 MySession.SetString("userName", userName);
24             }
25             var t1 = MySession.GetString("userName");
26             ViewBag.userName = userName;
27             return View();
28         }
29 
30         public IActionResult Index4()
31         {
32             string userName = MySession.GetString("userName");
33             ViewBag.userName = userName;
34             return View();
35         }
36         #endregion
37 
38     }

 

二. 高级

1. Session的扩展

(1).背景

  我们发现系统默认提供的Session方法,仅能存储byte、string、int类型,很不灵活,我们想能存储任何对象,这个时候就需要我们自行扩展。 对SessionExtensions类进行扩展

(2).利用Newtonsoft.Json进行扩展

  A. 扩展代码详见SessionExtensions中的Set和Get方法

  B. 案例详见Index5

(3).利用protobuf-net进行扩展

  A. 通过Nuget安装程序集【protobuf-net】,这是谷歌的一个程序集,序列化和反序列效率更高

  B. 扩展代码详见SessionExtensions中的Set2和Get2方法, 如果写入的是个类,类名上要加 [ProtoContract],属性上要加 [ProtoMember(1)] [ProtoMember(2)]等等, 如:UserInfor2

  C. 案例详见Index6

扩展代码:

 1   public static class SessionExtensions
 2     {
 3         #region 01-利用Newtonsoft.Json进行扩展
 4         public static void Set<T>(this ISession session, string key, T value)
 5         {
 6             session.SetString(key, JsonConvert.SerializeObject(value));
 7         }
 8 
 9         public static T Get<T>(this ISession session, string key)
10         {
11             var value = session.GetString(key);
12             return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
13         }
14         #endregion
15 
16         #region 02-利用protobuf-net进行扩展
17         public static void Set2<T>(this ISession session, string key, T value)
18         {
19             using (MemoryStream stream = new MemoryStream())
20             {
21                 Serializer.Serialize(stream, value);
22                 byte[] byteArrary = stream.ToArray();
23                 session.Set(key, byteArrary);
24             }
25         }
26 
27         public static T Get2<T>(this ISession session, string key)
28         {
29             byte[] byteArray = session.Get(key);
30             if (byteArray == null)
31             {
32                 return default(T);
33             }
34             else
35             {
36                 using (MemoryStream stream = new MemoryStream(byteArray))
37                 {
38                     return Serializer.Deserialize<T>(stream);
39                 }
40             }
41         }
42         #endregion
43     }

类代码:

 1  public class UserInfor1
 2     {
 3         public string id { get; set; }
 4         public string userName { get; set; }
 5     }
 6     [ProtoContract]
 7     public class UserInfor2
 8     {
 9         [ProtoMember(1)]
10         public string id { get; set; }
11         [ProtoMember(2)]
12         public string userName { get; set; }
13 
14     }

测试代码:

 1   #region 03-测试Newtonsoft.Json进行扩展
 2         public IActionResult Index5()
 3         {
 4             UserInfor1 user = HttpContext.Session.Get<UserInfor1>("u1");
 5             if (user == null)
 6             {
 7                 user = new UserInfor1();
 8                 user.id = "001";
 9                 user.userName = "Marren";
10                 HttpContext.Session.Set<UserInfor1>("u1", user);
11             }
12             ViewBag.id = user.id;
13             ViewBag.userName = user.userName;
14             return View();
15         }
16         #endregion
17 
18         #region 04-测试protobuf-net进行扩展
19         public IActionResult Index6()
20         {
21             UserInfor2 user = HttpContext.Session.Get2<UserInfor2>("u2");
22             if (user == null)
23             {
24                 user = new UserInfor2();
25                 user.id = "001";
26                 user.userName = "Marren";
27                 HttpContext.Session.Set2<UserInfor2>("u2", user);
28             }
29             ViewBag.id = user.id;
30             ViewBag.userName = user.userName;
31             return View();
32         }
33         #endregion

2.Session的性质

(1). Session的过期时间(多次访问将会被重置,默认过期时间为20分钟)

  如:options.IdleTimeout = TimeSpan.FromMinutes(120);

(2). 设置为true表示前端js等脚本无法读取cookie,防止了xss攻击(默认是true)

  如:options.Cookie.HttpOnly = true;

(3). Cookie是必须的(默认是false),可以覆盖上面的cookie策略

  如:options.Cookie.IsEssential = true;

3. 如何在普通类中使用Session对象?

(1).说明

  再普通类中如何调用Session,大约有两种思路:以构造函数的注入进去,但是如果这个类不是过滤器,使用的时候就需要实例化,又需要传入对象进去,如CheckLogin类,很麻烦,所以建议用第二种方案,直接吧实例好的Session传入进去,直接使用即可,如CheckLogin2.

(2). 方案一

  先统一注入, services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();,然后再在控制器中进行构造函数注入,但调用的时候还需要传入进去。

PS:过滤器中可以直接context.httpContext.Session来设置

(3). 方案二

  直接把实例好的Session传进去

 1  public class CheckLogin2
 2     {
 3         private ISession MySession { get; set; }
 4 
 5         public CheckLogin2(ISession mySession)
 6         {
 7             MySession = mySession;
 8         }
 9         public bool isLogin()
10         {
11             string userName = MySession.GetString("userName");
12             if (string.IsNullOrEmpty(userName))
13             {
14                 return false;
15             }
16             return true;
17         }
18     }

 

三. 进程外Session

   关于进程外Session的好处、解决了什么痛点问题,可以参考Asp.Net 中的进程外Session,去查阅。 (解决了IIS重启Session丢失的问题,解决了Session空间有限容易被挤爆的问题,但不能解决浏览器重启找不到Session的问题!)

  特别说明:基于SQLServer或Redis数据的进程外Session 的数据库方面的配置和前面分布式缓存章节数据库的配置完全相同,去查阅。

1. 基于SQLServer

 

 

 

2. 基于Redis

 

 

 

 

 

 

 

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 本人才疏学浅,用郭德纲的话说“我是一个小学生”,如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 

转载于:https://www.cnblogs.com/yaopengfei/p/11270816.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值