首先创建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>

原文链接

.NetCore Session.Redis (转载)的更多相关文章

  1. .NetCore Session.Redis

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

  2. 在 ASP.NET CORE 中使用 SESSION (转载)

    Session 是保存用户和 Web 应用的会话状态的一种方法,ASP.NET Core 提供了一个用于管理会话状态的中间件.在本文中我将会简单介绍一下 ASP.NET Core 中的 Session ...

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

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

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

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

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

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

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

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

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

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

  8. Session Redis Nginx

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

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

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

随机推荐

  1. 【代码笔记】iOS-JSONKit的使用

    一,工程图. 二,代码. #import "RootViewController.h" //为JSONKit添加头文件 #import "JSONKit.h" ...

  2. 一台电脑配置多个tomcat过程

    方法1:https://jingyan.baidu.com/article/76a7e409edbb4dfc3b6e1516.html 方法2:https://www.cnblogs.com/yiyi ...

  3. css选取table元素的第一列

    table tr td:first-child

  4. NUnit单元测试示例

    单元测试的用法 1.下载NUnit软件 安装后打开界面如图: 2.新建测试项目 添加类库项目并在NuGet管理包中添加NUnit 这里添加NuGet的NUnit包要注意保持版本和之前下载的NUnit软 ...

  5. OSGI企业应用开发(八)整合Spring和Mybatis框架(一)

    到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...

  6. ArcGIS 中取出面上最大的Z值的坐标点

    def MaxZ(shape):        line = shape.getPart(0)     pnt = line.next()     maxValue = float("-in ...

  7. Nginx的性能优化

    1.优化worker进程个数: 在高并发.高访问量的WEB服务场景,需要事先启动更多的nginx进程,以保证快速响应并处理大量并发用户的请求,优化nginx进程个数的配置项就是,在nginx.conf ...

  8. vue与原生混合开发

    前段时间,做了一个混合开发的项目,主要是以vue框架开发h5页面,使用cordova作为中间沟通桥梁,实现了h5与安卓.iOS的混合开发,由于从事iOS开发,h5也是刚接触不久,很多深入原理还不太清楚 ...

  9. zabbix系列之安全

    https://blog.csdn.net/xiaoyu_0217/article/details/73500125 存在问题: 1)zabbix的Admin口令太弱或使用默认口令(Admin/zab ...

  10. shell 的echo和 printf

    shell的echo指令是输出语句  就好比Python的print 在显示字符串的时候可以省略双引号  但是最好还是带上 echo ' Ti is a dashaobing' echo Ti is ...