.NetCore Session.Redis (转载)
首先创建ASP.NET CORE Web项目,然后按如下顺序操作。
1.添加nuget程序包:
Microsoft.AspNetCore.Session; Microsoft.AspNetCore.DataProtection.Redis; Microsoft.Extensions.Caching.Redis.Core; Microsoft.Extensions.Caching.Redis Microsoft.AspNetCore.Http; //使用Session时有扩展方法
2.在appsettings.json中添加Redis配置:
{"Logging": {"LogLevel": {"Default": "Warning"}},"WebConfig": {"Redis": {"Connection": "127.0.0.1:6379,defaultdatabase=1",//"Connection": "127.0.0.1:6379,allowAdmin=true,password=123456,defaultdatabase=5","InstanceName": "Core_Redis_Session_"},"SessionTimeOut": "30" //session过期时长,分钟},"AllowedHosts": "*" }
3.在startup.cs类中,按如下例子添加代码:
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) {services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);/** Microsoft.AspNetCore.Session;* Microsoft.AspNetCore.DataProtection.Redis;* Microsoft.Extensions.Caching.Redis.Core;* Microsoft.Extensions.Caching.Redis* Microsoft.AspNetCore.Http; //使用Session时有扩展方法*/#region 使用Redis保存Sessionvar redisConn = Configuration["WebConfig:Redis:Connection"];var redisInstanceName = Configuration["WebConfig:Redis:InstanceName"];//Session 过期时长分钟var sessionOutTime = Configuration.GetValue<int>("WebConfig:SessionTimeOut", 30);//var redis = StackExchange.Redis.ConnectionMultiplexer.Connect(redisConn);//services.AddDataProtection().PersistKeysToRedis(redis, "DataProtection-Test-Keys");services.AddDistributedRedisCache(option =>{//redis 连接字符串option.Configuration = redisConn;//redis 实例名option.InstanceName = redisInstanceName;});#endregion//添加Session并设置过期时长services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(sessionOutTime); });}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) {app.UseSession();app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");}); }
4.在控制器HomeController中添加:
public class HomeController : Controller {public IActionResult Index(){ViewData["UserName"] = this.HttpContext.Session.GetString("UserName");ViewData["PassWord"] = this.HttpContext.Session.GetString("PassWord");return View();}[HttpPost]public NoContentResult Add(string userName, string pwd){this.HttpContext.Session.SetString("UserName", userName);this.HttpContext.Session.SetString("PassWord", pwd);return NoContent();} }
5.在View/Index.cshtml添加如下代码:
<form method="post" action="../Home/Add"><div><input name="username" id="username" type="text" value="@ViewData["UserName"]" /><input name="pwd" id="pwd" type="password" value="" /><input type="submit" value="更新" /><h1>提交用户名称为:@ViewData["UserName"] 密码:@ViewData["PassWord"]</h1><a href="javascript:void(0);" onclick="window.location.reload();">刷新显示最新值</a></div> </form>
原文链接
.NetCore Session.Redis (转载)相关推荐
- .NetCore Session.Redis
首先创建ASP.NET CORE Web项目,然后按如下顺序操作. 1.添加nuget程序包: Microsoft.AspNetCore.Session;Microsoft.AspNetCore.Da ...
- 在spring MVC项目中集成Spring session redis (使用spring session框架,redis作为存储缓存)...
2019独角兽企业重金招聘Python工程师标准>>> 1.为项目增加以来 pom.xml中使用 <!-- spring session 单点登录 --> //本项目使 ...
- php session redis 配置
相信很多人用过redis存放session,可能有些人还不是很清楚,这里说一下如何配置php的session存放在redis里面?然后如何测试出效果? 具体环境: 一台apache+php的服务器(y ...
- Spring Security + Session Redis——JSON序列化错误[The class xxx and name of xxx is not whitelisted. ]解决方案
前置 Spring Security + Spring Session + Redis--[SecurityContext]和[AuthenticationToken]JSON反序列化问题解决方案 问 ...
- Spring Security + Spring Session Redis——【SecurityContext】和【AuthenticationToken】JSON反序列化问题解决方案
前置 Spring Session + Redis--自定义JSON序列化解决方案 问题描述 Spring Security + Spring Session使用中Redis,默认序列化方式是JdkS ...
- Spring Session Redis——自定义JSON序列化解决方案
问题描述 Spring Session + Redis--自定义JSON序列化解决方案 问题分析 RedisHttpSessionConfiguration中,Bean名称必须是springSessi ...
- Spring Boot——Spring Session Redis整合Spring Security时错误【RedisConnectionFactory is required】解决方案
问题描述 异常栈栈底 Caused by: java.lang.IllegalStateException: RedisConnectionFactory is requiredat org.spri ...
- 单点登录实现(spring session+redis完成session共享)
一.前言 项目中用到的SSO,使用开源框架cas做的.简单的了解了一下cas,并学习了一下 单点登录的原理,有兴趣的同学也可以学习一下,写个demo玩一玩. 二.工程结构 我模拟了 sso的客户端和s ...
- php session redis db,php session redis 配置
具体环境: 一台apache+php的服务器(yum安装remi源及配置 httpd-2.2.15 php-5.4.45) 一台redis服务器(yum安装remi源及配置 redis-3.2.6) ...
- Spring Session, Redis 实现微服务 Session 共享
在微服务中,实现各个微服务模块之间的Session共享,可以通过 Spring Session 和 Redis 技术实现. 本实例直接在之前的两个微服务中添加同样的配置,然后测试两个接口查看 Sess ...
最新文章
- 逻辑回归之案例:癌症分类预测-良/恶性乳腺癌肿瘤预测
- php 未定义偏移,未定义的偏移PHP错误,尝试从文件读取
- python装饰器有几种_python几种装饰器的用法
- Redux源码分析(一)
- linux中断处理函数参数,第9章 设置ISR(中断处理函数)
- 本科是最底层?学历真的那么重要么?
- 代写php代码作业,代写phpmyadmin留学生作业、代做SQL语言作业、SQL程序设计作业调试、代做PHP script作业...
- MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
- 为什么要用implements Serializable
- android捆绑demo_Android捆绑通知
- javascript:document.all document.getElementsByName
- 2021年浙江省职业院校技能大赛“新华三杯”大数据技术与应用比赛 比赛经验
- H5游戏忆童年—承载梦想的纸飞机回来了吗?
- 211逆袭浙大-计算机及相关衍生专业保研之路纪实(深度长文,收藏了)
- Toast的几种用法
- linux热迁移137error VMware vCenter Converter 问题小结
- 计算机操作系统u盘的安装方法,怎么直接用u盘装系统操作教程
- idea发现git出现cannot run git:cannot identify version of git executable :no response
- 模拟电路50(开关电容滤波器)
- U3D AudioSource 完整音效截取部分
热门文章
- centos7 firewalld
- BAT脚本每隔30秒创建一个512兆文件(测试磁盘监控用)
- centos7 crontab 定时执行python任务不执行的原因及解决办法
- Codeforces #430 dv.2 842A ,842B
- Cocos2d-x动画播放(序列帧)
- 顶点计划:寝室作息讨论
- HDU 2457 DNA repair(AC自动机 + DP)题解
- 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题2---CMAKE配置问题
- 使用bootbox.js(二级务必提交书面和数字到数字中国)
- Linux编译安装PHP Mysql Nginx