标签:过期 int timeout new 扩展 配置文件 ase 哈哈哈 logic
.net core 3.1中将session持久化到redis,从core1.0开始,2.0/2.1,3.0/3.1每次搭建开发框架都因为session的问题搞的头大,次次踩坑,花了半天时间再次捋了一遍,发出来如果大家有跟我一样,希望能帮到你,另外如果有帮到你,记得点赞哈!!!
1.需引入的库文件
1.1:Microsoft.AspNetCore.Http.Extensions,因为.net core中的session默认只能存储字节,所以引入该库是为了解决session存储字符串
2.Microsoft.AspNetCore.Session,Microsoft.Extensions.Caching.Redis,针对session扩展
3.StackExchange.Redis为redis驱动
4.Newtonsoft.Json我自己项目常使用的序列化工具,可根据自己的需求使用
项目针对session持久化redis的配置
"SessoinConfig": { // Session配置 "RedisCacheConfig": { "ConnectionString": "ip:端口,ssl=false,password=密码,defaultdatabase=redis库索引", "InstanceName": "自定义实例名" }, "SessionTimeOut": 60 //session过期时长,分钟 }
注入实体类:
/// <summary> /// Session缓存配置 /// </summary> public class SessoinConfig { /// <summary> /// session过期时长,分钟 /// </summary> public int SessionTimeOut { get; set; } /// <summary> /// 实例库索引 /// </summary> public RedisCacheConfigData RedisCacheConfig { get; set; } } /// <summary> /// Session持久化Redis配置 /// </summary> public class RedisCacheConfigData { /// <summary> /// Redis连接字符串 /// </summary> public string ConnectionString { get; set; } /// <summary> /// Redis实例名称 /// </summary> public string InstanceName { get; set; } }
IServiceCollection扩展
/// <summary> /// Session持久化至Redis /// </summary> /// <param name="services"></param> /// <param name="Configuration"></param> public static void UseSession(this IServiceCollection services, SessoinConfig sessoinConfig) { services.AddSingleton<IDistributedCache>( serviceProvider => new RedisCache(new RedisCacheOptions { Configuration = sessoinConfig.RedisCacheConfig.ConnectionString, InstanceName = sessoinConfig.RedisCacheConfig.InstanceName })); // 添加Session并设置过期时长 services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(sessoinConfig.SessionTimeOut); }); }
Startup服务添加容器方法ConfigureServices
// 读取Session配置文件 IConfigurationSection sessionCacheSections = Configuration.GetSection("SessoinConfig"); SessoinConfig sessionCacheConfig = sessionCacheSections.Get<SessoinConfig>(); services.UseSession(sessionCacheConfig);
Startup运行时方法Configure使用Session
// 使用Session app.UseSession();
自定义Session扩展
/// <summary> /// Session扩展 /// </summary> public static class SessionExtensions { /// <summary> /// 添加Session /// </summary> /// <param name="session"></param> /// <param name="key"></param> /// <param name="value"></param> public static void SetObjectAsJson(this ISession session, string key, object value) { session.SetString(key, JsonConvert.SerializeObject(value)); } /// <summary> /// 添加Session /// </summary> /// <param name="session"></param> /// <param name="key"></param> /// <param name="value"></param> public static void SetObjectAsJson<T>(this ISession session, string key, T value) { session.SetString(key, JsonConvert.SerializeObject(value)); } /// <summary> /// 获取Session /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="key"></param> /// <returns></returns> public static T GetObjectFromJson<T>(this ISession session, string key) { var value = session.GetString(key); return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value); } /// <summary> /// 删除Session /// </summary> /// <param name="session"></param> /// <param name="key"></param> public static void RemoveSession(this ISession session, string key) { var value = session.GetString(key); if (value != null) { session.Remove(key); } } }
例如我存储登录验证码
/// <summary> /// 用户登录验证码 /// </summary> protected string ValidateCodeData { get { return HttpContext.Session.GetObjectFromJson<string>(LoginValidateCodeSessionKey); } set { HttpContext.Session.SetObjectAsJson<string>(LoginValidateCodeSessionKey, value); } }
/// <summary> /// 获取验证码 /// </summary> /// <returns></returns> public IActionResult ValidateCode() { System.IO.MemoryStream ms = _logic.LoginValidateCode(out string code, 4); ValidateCodeData = code; Response.Body.Dispose(); return File(ms.ToArray(), @"image/png"); }
欢迎转载,转载请保留出处,哈哈哈
.net core 3.0/3.1session持久化redis,session id为空
标签:过期 int timeout new 扩展 配置文件 ase 哈哈哈 logic
原文地址:https://www.cnblogs.com/lixiaojun1009/p/12890498.html