ASP.NET Core 使用Redis存储Session
2017-10-25 15:30
731 查看
前言
Session是我们在web开发中经常使用的对象,它默认是存在本机的,但是在ASP.NET Core中我们可以十分方便的将Session的存储介质改为分布式缓存(Redis)或者数据库(SqlServer)。分布式的缓存可以提高ASP.NET Core 应用的性能和可伸缩性 ,尤其是在托管在云中或服务器场环境中使用
ASP.NET Core 已经为我们实现了Redis缓存。1.配置服务
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //添加redis services.AddDistributedRedisCache(options => { options.Configuration = "localhost"; }); //添加session services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(10); //session活期时间 options.Cookie.HttpOnly = true;//设为httponly }); }
2.启用Session
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //使用session app.UseSession(); ... }
对Session进行操作
使用HttpContext.Session来获取Session对象
例:
HttpContext.Session.SetString("userid","1000");
运行项目,可以看到redis已经有我们刚刚访问所创建的Session
相关文章推荐
- 详解Asp.net Core 使用Redis存储Session
- [转]Asp.net Core 使用Redis存储Session
- ASP.NET Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- asp.net使用redis存储session(RedisSessionStateProvider)
- Asp.Net Core 集成 Hangfire 配置使用 Redis 存储
- Asp.net Core中使用Redis 来保存Session
- Asp.net Core中使用Redis 来保存Session, 读取配置文件
- asp.net core 使用 Redis 和 Protobuf 进行 Session 缓存
- ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存
- 分布式系统状态下redis存储asp.net session使用第三方Providers驱动
- 在 ASP.NET CORE 中使用 SESSION
- ASP.NET Core 使用 Redis 客户端
- 解析Asp.net Core中使用Session的方法
- ASP.NET MVC 使用Redis共享Session
- Asp.net Core中使用Session
- asp.net core 使用 Redis 和 Protobuf
- ASP.NET下跨应用共享Session和使用Redis进行Session托管