首先创建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保存Session
var redisConn = Configuration["WebConfig:Redis:Connection"];
var redisInstanceName = Configuration["WebConfig:Redis:InstanceName"];
//Session 过期时长分钟
var sessionOutTime = Configuration.GetValue<int>("WebConfig:SessionTimeOut", ); //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>

  

源代码下载:https://github.com/842549829/Session.Redis.Web

.NetCore Session.Redis的更多相关文章

  1. .NetCore Session.Redis (转载)

    首先创建ASP.NET CORE Web项目,然后按如下顺序操作. 1.添加nuget程序包: Microsoft.AspNetCore.Session; Microsoft.AspNetCore.D ...

  2. Spring boot配合Spring session(redis)遇到的错误

    背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...

  3. 单点登录实现(spring session+redis完成session共享)

    一.前言 项目中用到的SSO,使用开源框架cas做的.简单的了解了一下cas,并学习了一下 单点登录的原理,有兴趣的同学也可以学习一下,写个demo玩一玩. 二.工程结构 我模拟了 sso的客户端和s ...

  4. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享

    小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...

  5. Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程

    待解决的问题 Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程 解决办法 为spring session添加spr ...

  6. SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群

    session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...

  7. Session Redis Nginx

    Session + Redis + Nginx 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机 ...

  8. Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace

    现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...

  9. netcore 使用redis session 分布式共享

    首先准备redis服务器(docker 和redis3.0内置的哨兵进行高可用设置) 网站配置Redis作为存储session的介质(配置文件这些略).然后可以了解一下MachineKey这个东西.( ...

随机推荐

  1. 一个讲课截屏 清明DAY2

    灰常混乱 放弃吧........ 不断做平方差公式 到i时,前面已经求出之前数字的逆元了 r是一个比i小的数 第四行×i,r 的逆元 BSGS 暴力枚举枚举到Φ(m)个

  2. mycat工作原理

    Mycat的原理并不复杂,复杂的是代码,如果代码也不复杂,那么早就成为一个传说了. Mycat的原理中最重要的一个动词是“拦截”,它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析: ...

  3. easyui dialog 表单提交,弹框初始化赋值,dialog实现

    //定义弹框html<div id="delete" class="easyui-dialog" title="Delete" dat ...

  4. GoldenGate 12.3 MA架构介绍系列(2) - 数据同步测试

    安装配置可参考上一篇:http://www.cnblogs.com/margiex/p/8071957.html 安装完成之后,会自动启动ServiceManager服务,此时,可以通过浏览器访问. ...

  5. Java用Gson遍历json所有节点

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...

  6. MyEclipse如何配置Struts2源码的框架压缩包

    1.MyEclipse如何配置Struts2源码的框架压缩包 如本机的Struts2框架压缩包路径为:D:\MyEclipseUserLibraries\struts\struts-2.3.15.3- ...

  7. thinkphp 随笔

     'TMPL_CACHE_ON' => false,//禁止模板编译缓存 'HTML_CACHE_ON' => false,//禁止静态缓存 

  8. intel FPGA使用

    https://www.altera.com/documentation/swn1503506366945.html https://files.cnblogs.com/files/shaohef/o ...

  9. 自动发现实现url+响应时间监控

    url自动发现脚本: [root@jenkins scripts]# cat  urlDiscovery.py #!/usr/bin/env python #coding:utf-8 import o ...

  10. css 元素居中

    css 4种常见实现元素居中的办法: 1.通过 margin 属性调整 : { position: absolute; top: 50%; left: 50%; margin-left: 盒子的一半: ...