标签:过期 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过期时长,分钟
}
注入实体类:
///
/// Session缓存配置
///
public class SessoinConfig
{
///
/// session过期时长,分钟
///
public int SessionTimeOut { get; set; }
///
/// 实例库索引
///
public RedisCacheConfigData RedisCacheConfig { get; set; }
}
///
/// Session持久化Redis配置
///
public class RedisCacheConfigData
{
///
/// Redis连接字符串
///
public string ConnectionString { get; set; }
///
/// Redis实例名称
///
public string InstanceName { get; set; }
}
IServiceCollection扩展
///
/// Session持久化至Redis
///
///
///
public static void UseSession(this IServiceCollection services, SessoinConfig sessoinConfig)
{
services.AddSingleton(
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();
services.UseSession(sessionCacheConfig);
Startup运行时方法Configure使用Session
// 使用Session
app.UseSession();
自定义Session扩展
///
/// Session扩展
///
public static class SessionExtensions
{
///
/// 添加Session
///
///
///
///
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
///
/// 添加Session
///
///
///
///
public static void SetObjectAsJson(this ISession session, string key, T value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
///
/// 获取Session
///
///
///
///
///
public static T GetObjectFromJson(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject(value);
}
///
/// 删除Session
///
///
///
public static void RemoveSession(this ISession session, string key)
{
var value = session.GetString(key);
if (value != null)
{
session.Remove(key);
}
}
}
例如我存储登录验证码
///
/// 用户登录验证码
///
protected string ValidateCodeData
{
get { return HttpContext.Session.GetObjectFromJson(LoginValidateCodeSessionKey); }
set { HttpContext.Session.SetObjectAsJson(LoginValidateCodeSessionKey, value); }
}
///
/// 获取验证码
///
///
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